設(shè)為首頁收藏本站Access中國

Office中國論壇/Access中國論壇

 找回密碼
 注冊

QQ登錄

只需一步,快速開始

12下一頁
返回列表 發(fā)新帖
查看: 12168|回復: 13
打印 上一主題 下一主題

[模塊/函數(shù)] 使用CDO發(fā)送郵件(類模塊)

[復制鏈接]
跳轉(zhuǎn)到指定樓層
1#
發(fā)表于 2008-3-1 14:37:37 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
  1. Option Compare Database
  2. Option Explicit
  3. '                           \\\|///
  4. '                         \\  - -  //
  5. '                          (  @ @  )
  6. '━━━━━━━━━━━━oOOo-(_)-oOOo━━━━━━━━━━━━━━
  7. '-類名稱:       SendMail
  8. '-功能描述:     發(fā)送郵件
  9. '-參考:
  10. '-使用注意:
  11. '-兼容性:       2000,XP,2003
  12. '-作者:         fan0217@tom.com
  13. '-更新日期:    2007-08-22
  14. '                            Oooo
  15. '━━━━━━━━━━oooO━-(   )━━━━━━━━━━━━━━━━━
  16. '                    (   )   ) /
  17. '                     \ (   (_/
  18. '                      \_)
  19. Private Const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing"
  20. Private Const cdoSendUsingPort = 2
  21. Private Const cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
  22. Private Const cdoSMTPServerPort = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"
  23. Private Const cdoSMTPConnectionTimeout = "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
  24. Private Const cdoSMTPAuthenticate = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
  25. Private Const cdoBasic = 1
  26. Private Const cdoSendUserName = "http://schemas.microsoft.com/cdo/configuration/sendusername"
  27. Private Const cdoSendPassword = "http://schemas.microsoft.com/cdo/configuration/sendpassword"
  28. Private objConfig ' As CDO.Configuration
  29. Private objMessage ' As CDO.Message
  30. Private Fields ' As ADODB.Fields
  31. Private strSMTPServer As String
  32. Private strSendUserName As String
  33. Private strSendPassword As String
  34. Private strFromMail As String
  35. Private intSMTPConnectionTimeout As Integer
  36. Private intSMTPServerPort As Integer

  37. Public Function Send(toMail As String, subject As String, textBody As String, Optional attachment As String = "") As Boolean
  38.     SendInitialize
  39.     With objMessage
  40.         .to = toMail '接收者的郵件地址
  41.         .From = FromMail '發(fā)送人的郵件地址
  42.         .subject = subject '標題
  43.         .textBody = textBody '正文
  44.         If attachment <> "" Then
  45.             .addAttachment attachment '郵件附件
  46.         End If
  47.         .Send
  48.     End With
  49.     Send = True
  50. End Function

  51. Private Sub SendInitialize()
  52.     Set objConfig = CreateObject("CDO.Configuration")
  53.     Set Fields = objConfig.Fields
  54.     With Fields
  55.         .Item(cdoSendUsingMethod) = cdoSendUsingPort
  56.         .Item(cdoSMTPServer) = SMTPServer
  57.         .Item(cdoSMTPServerPort) = SMTPServerPort
  58.         .Item(cdoSMTPConnectionTimeout) = SMTPConnectionTimeout
  59.         .Item(cdoSMTPAuthenticate) = cdoBasic
  60.         .Item(cdoSendUserName) = SendUserName
  61.         .Item(cdoSendPassword) = SendPassword
  62.         .Update
  63.     End With
  64.     Set objMessage = CreateObject("CDO.Message")
  65.     Set objMessage.Configuration = objConfig
  66. End Sub

  67. '可用的外部郵件服務器域名
  68. Public Property Get SMTPServer() As String
  69.     SMTPServer = strSMTPServer
  70. End Property
  71. Public Property Let SMTPServer(ByVal value As String)
  72.     strSMTPServer = value
  73. End Property

  74. '郵件服務器的用戶名
  75. Public Property Get SendUserName() As String
  76.     SendUserName = strSendUserName
  77. End Property
  78. Public Property Let SendUserName(ByVal value As String)
  79.     strSendUserName = value
  80. End Property

  81. '郵件服務器的密碼
  82. Public Property Get SendPassword() As String
  83.     SendPassword = strSendPassword
  84. End Property
  85. Public Property Let SendPassword(ByVal value As String)
  86.     strSendPassword = value
  87. End Property

  88. '發(fā)件人的地址(要和SMTP相同)
  89. Public Property Get FromMail() As String
  90.     FromMail = strFromMail
  91. End Property
  92. Public Property Let FromMail(ByVal value As String)
  93.     strFromMail = value
  94. End Property

  95. Public Property Get SMTPConnectionTimeout() As Integer
  96.     SMTPConnectionTimeout = intSMTPConnectionTimeout
  97. End Property
  98. Public Property Let SMTPConnectionTimeout(ByVal value As Integer)
  99.     intSMTPConnectionTimeout = value
  100. End Property

  101. Public Property Get SMTPServerPort() As Integer
  102.     SMTPServerPort = intSMTPServerPort
  103. End Property
  104. Public Property Let SMTPServerPort(ByVal value As Integer)
  105.     intSMTPServerPort = value
  106. End Property

  107. Private Sub Class_Initialize()
  108.     SMTPServerPort = 25
  109.     SMTPConnectionTimeout = 10
  110. End Sub

  111. Private Sub Class_Terminate()
  112.     Set Fields = Nothing
  113.     Set objMessage = Nothing
  114.     Set objConfig = Nothing
  115. End Sub
復制代碼
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏5 分享分享 分享淘帖 訂閱訂閱
2#
 樓主| 發(fā)表于 2008-3-1 14:38:30 | 只看該作者
  1. Sub Test()
  2. Dim s As New SendMail
  3. s.SMTPServer = "SMTP.tom.com"
  4. s.SendUserName = "fan0217"
  5. s.SendPassword = "**********"
  6. s.FromMail = "fan0217@tom.com"
  7. s.Send "fan0217@tom.com", "測試郵件", "收到請回復!--" & Now
  8. Set s = Nothing
  9. End Sub
復制代碼
3#
發(fā)表于 2008-3-1 14:41:50 | 只看該作者
沙發(fā)
學習學習
4#
發(fā)表于 2008-3-1 15:05:15 | 只看該作者
5#
發(fā)表于 2008-3-1 15:48:17 | 只看該作者
.

點擊這里給我發(fā)消息

6#
發(fā)表于 2008-3-1 16:37:25 | 只看該作者
呵呵, 剛看到一個CDO例程, 又白白收了一個CDO的類庫, 趕快收藏了!! 謝謝分享
7#
發(fā)表于 2009-12-23 20:31:45 | 只看該作者
謝謝分享
8#
發(fā)表于 2010-12-1 15:23:42 | 只看該作者
太經(jīng)典了。
9#
發(fā)表于 2015-3-26 23:19:45 | 只看該作者
10#
發(fā)表于 2016-2-2 12:07:18 | 只看該作者
要仔細學習下,謝謝
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則

QQ|站長郵箱|小黑屋|手機版|Office中國/Access中國 ( 粵ICP備10043721號-1 )  

GMT+8, 2025-7-13 05:06 , Processed in 0.107249 second(s), 33 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回復 返回頂部 返回列表