简体   繁体   English

CDO.Message Email 来自 VBA 中的 Office 365 帐户

[英]CDO.Message Email from Office 365 Account in VBA

I'm trying to send an email from an account on Office 355 using CDO.message in VBA to automate some email tasks in Microsoft Access.我正在尝试使用 VBA 中的 CDO.message 从 Office 355 上的帐户发送 email 以自动化 Microsoft Access 中的一些 email 任务。

The code is from this source.代码来自这个来源。

I get我明白了

"The transport failed to connect to the server" “传输无法连接到服务器”

From my research, it seems to be an issue with TLS authentication.根据我的研究,这似乎是 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

EDIT: The email needs to be sent from the same email address no matter the user, and thus, I don't believe using Outlook will work.编辑:无论用户如何,email 都需要从相同的 email 地址发送,因此,我不相信使用 Outlook 会起作用。

EDIT 2: It seems to be that ports 25 and 465 are blocked by my ISP.编辑2:我的ISP似乎阻止了端口25和465。 Using telnet I was able to get a response from port 587, however, I get the same error as before (except this time I get it immediately instead of after a long delay).使用 telnet 我能够从端口 587 获得响应,但是,我得到了与以前相同的错误(除了这次我立即得到它,而不是经过长时间的延迟)。 Likely due to lack of TLS authentication.可能是由于缺少 TLS 身份验证。

This is an alternative that I've used before:这是我以前使用过的替代方法:

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

And here are the required references: vba references以下是所需的参考资料: vba 参考资料

I've used this from a MS Access form where the user enters text that goes into the message, then clicks a "Send Email" button to send via Outlook.我从一个 MS Access 表单中使用了它,用户在该表单中输入进入消息的文本,然后单击“发送电子邮件”按钮通过 Outlook 发送。 Hopefully this helps!希望这会有所帮助!

Your settings are correct.你的设置是正确的。

What you miss is, most likely, to create and use an App Password .您最有可能错过的是创建和使用App Password

Official documentation:官方文档:

Manage app passwords for two-step verification 管理应用密码以进行两步验证

As a very first step, open a command prompt and verify you can reach the server:作为第一步,打开命令提示符并验证您是否可以访问服务器:

telnet smtp.office365.com 25

This shall at once return something like this:这将立即返回如下内容:

220 HE1PR09CA0075.outlook.office365.com Microsoft ESMTP MAIL Service ready at Fri, 19 Feb 2021 18:48:46 +0000

If that times out, check your firewall and router settings.如果超时,请检查您的防火墙和路由器设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM