这是我的环境:Windows Server 2003、Microsoft Access 2003、Microsoft VB 6.5 尝试使用 CDO.Message 从 Access 发送电子邮件。这是我的代码的一部分:
Private Sub btnTestEmail_Click()
On Error GoTo SendMail_Error:
Dim Mailmsg As Object
Dim mailconf As Object
Dim McFields As Object
Dim strSchemas As String
Set Mailmsg = CreateObject("CDO.Message")
Set mailconf = CreateObject("CDO.Configuration")
Set McFields = mailconf.Fields
strSchemas = "http://schemas.microsoft.com/cdo/configuration/"
With McFields
.Item(strSchemas & "sendusing") = 2
.Item(strSchemas & "smtpserver") = "smtp.gmail.com"
.Item(strSchemas & "smtpserverport") = 465
.Item(strSchemas & "smtpauthenticate") = 1
.Item(strSchemas & "sendusername") = "my_email@gmail.com"
.Item(strSchemas & "sendpassword") = "my_gmail_password"
.Item(strSchemas & "smtpconnectiontimeout") = 60
.Item(strSchemas & "smtpusessl") = 1
.Update
End With
Set Mailmsg.Configuration = mailconf
With Mailmsg
.TextBody = "Test email body text"
.Subject = "Test email subject"
.To = "target_email@gmail.com"
.from = "my_email@gmail.com"
'.AddAttachment "D:\test.pdf"
.Fields("urn:schemas:mailheader:disposition-notification-to") = "my_email@gmail.com"
.Fields("urn:schemas:mailheader:return-receipt-to") = "my_email@gmail.com"
' Set delivery status notification (DSN)
' Name Value Description
' cdoDSNDefault 0 No DSN commands are issued.
' cdoDSNNever 1 No DSN commands are issued.
' cdoDSNFailure 2 Return a DSN if delivery fails.
' cdoDSNSuccess 4 Return a DSN if delivery succeeds.
' cdoDSNDelay 8 Return a DSN if delivery is delayed.
' cdoDSNSuccessFailOrDelay 14 Return a DSN if delivery succeeds, fails, or is
.DSNOptions = 0
.Fields.Update
.Send
End With
MsgBox "Message Sent", vbOKOnly
Set Mailmsg = Nothing
Set mailconf = Nothing
Set McFields = Nothing
Exit Sub
SendMail_Error:
MsgBox Err.Description, vbOKOnly
End Sub
使用 DSNOptions = 0 效果很好,但我想获得送达通知(而不是阅读通知)。如果我将 DSNOptions 设置为任何允许的非零值,则电子邮件甚至不会到达目标电子邮件,并且我的电子邮件也不会收到任何通知。奇怪的是,如果我设置不存在的目标电子邮件(故意),即使 DSNOptions = 0,我也会收到传递不成功的通知。我是否在代码中遗漏了什么?在多个其他网站上发现有人声称此代码有效,但使用其他 smtp 服务器。任何帮助表示赞赏。