问题描述
我正在尝试使用 VBA 中的 cdo.message 从 Office 355 上的帐户发送电子邮件,以自动执行 Microsoft Access 中的某些电子邮件任务。
我明白
“传输无法连接到服务器”
根据我的研究,这似乎是 TLS 身份验证的问题。
Dim objMessage,objConfig,fields
Set objMessage = New cdo.message
Set objConfig = New CDO.Configuration
Set fields = objConfig.fields
With fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 '465
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "noreply@domain.ca"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "passowrd"
'.Item("http://schemas.microsoft.com/cdo/configuration/sendtls") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Update
End With
Set objMessage.Configuration = objConfig
With objMessage
.subject = "Test Message"
.From = "noreply@domain.ca"
.To = "noreply@domain.ca"
.HTMLBody = "Test Message"
End With
objMessage.Send
编辑:无论用户如何,电子邮件都需要从相同的电子邮件地址发送,因此,我不相信使用 Outlook 会起作用。
编辑 2:我的 ISP 似乎阻止了端口 25 和 465。使用 telnet 我能够从端口 587 得到响应,但是,我得到与以前相同的错误(除了这次我立即得到它而不是经过很长时间的延迟)。可能是因为缺少 TLS 身份验证。
解决方法
这是我以前使用过的替代方法:
Private Sub btnSendEmail_Click()
Dim olObject As Object
Dim mail As Object
Dim address As String,subject As String,body As String
Set olObject = New Outlook.Application
Set mail = olObject.CreateItem(olMailItem)
address = "fakeemail@test.com"
subject = "THIS IS THE EMAILS SUBJECT LINE"
body = "THIS GOES INTO EMAILS MESSAGE BODY"
mail.To = address
mail.subject = subject
mail.HTMLBody = body
mail.Send
If MsgBox("Email Sent!",vbExclamation + vbOKOnly,"Submitted") = vbOK Then: Exit Sub
End Sub
以下是所需的参考资料: vba references
我在 MS Access 表单中使用过这个,用户在其中输入进入邮件的文本,然后单击“发送电子邮件”按钮以通过 Outlook 发送。希望这会有所帮助!
,您的设置正确。
您最有可能错过的是创建和使用应用密码。
官方文档:
Manage app passwords for two-step verification
作为第一步,打开命令提示符并验证您可以访问服务器:
telnet smtp.office365.com 25
这将立即返回如下内容:
220 HE1PR09CA0075.outlook.office365.com Microsoft ESMTP MAIL Service ready at Fri,19 Feb 2021 18:48:46 +0000
如果超时,请检查您的防火墙和路由器设置。