Office中国论坛/Access中国论坛

标题: 使用CDO发送邮件(类模块) [打印本页]

作者: fan0217    时间: 2008-3-1 14:37
标题: 使用CDO发送邮件(类模块)
  1. Option Compare Database
  2. Option Explicit
  3. '                           \\\|///
  4. '                         \\  - -  //
  5. '                          (  @ @  )
  6. '━━━━━━━━━━━━oOOo-(_)-oOOo━━━━━━━━━━━━━━
  7. '-类名称:       SendMail
  8. '-功能描述:     发送邮件
  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 '发送人的邮件地址
  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. '发件人的地址(要和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
复制代码

作者: fan0217    时间: 2008-3-1 14:38
  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
复制代码

作者: fswxs    时间: 2008-3-1 14:41
沙发
学习学习
作者: huangqinyong    时间: 2008-3-1 15:05

作者: liwen    时间: 2008-3-1 15:48
.
作者: tmtony    时间: 2008-3-1 16:37
呵呵, 刚看到一个CDO例程, 又白白收了一个CDO的类库, 赶快收藏了!! 谢谢分享
作者: c101    时间: 2009-12-23 20:31
谢谢分享
作者: littlekey    时间: 2010-12-1 15:23
太经典了。
作者: 杨向宇    时间: 2015-3-26 23:19
fan0217 发表于 2008-3-1 14:38

非常实用
作者: ringo66666    时间: 2016-2-2 12:07
要仔细学习下,谢谢
作者: 764300778    时间: 2016-2-3 01:12
eeee
作者: 764300778    时间: 2016-3-3 21:04
eeee
作者: yyalm    时间: 2017-6-22 14:40
66666666666666666
作者: access新新新手    时间: 2017-11-21 12:08
谢谢分享




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3