我有下面的代码可以使用 CDO 从 VBA 宏发送邮件。我在代码中遇到错误:
Transport failed To connect to server Error
我正在从 Gmail SMTP 服务发送邮件。看起来配置设置正确,但不知何故它不起作用。
Sub Email()
Dim CDO_Mail As Object
Dim CDO_Config As Object
Dim SMTP_Config As Variant
Dim strSubject As String
Dim strFrom As String
Dim strTo As String
Dim strCc As String
Dim strBcc As String
Dim strBody As String
strSubject = "Results from Excel Spreadsheet"
strFrom = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f8808080b89f95999194d69b9795" rel="noreferrer noopener nofollow">[email protected]</a>"
strTo = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="98e0e0e0d8fff5f9f1f4b6fbf7f5" rel="noreferrer noopener nofollow">[email protected]</a>"
strBody = "The total results are: 167"
Set CDO_Mail = CreateObject("CDO.Message")
Set CDO_Config = CreateObject("CDO.Configuration")
CDO_Config.Load -1
Set SMTP_Config = CDO_Config.Fields
With SMTP_Config
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2babababa82a5afa3abaeeca1adaf" rel="noreferrer noopener nofollow">[email protected]</a>"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxx"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Update
End With
With CDO_Mail
Set .Configuration = CDO_Config
End With
CDO_Mail.Subject = strSubject
CDO_Mail.From = strFrom
CDO_Mail.To = strTo
CDO_Mail.TextBody = strBody
CDO_Mail.Send
End Sub
最佳答案
该代码对我来说完全正常(从 Gmail发送 Gmail) - 因此您需要检查以下内容:
- 尝试使用端口 587 和端口 465 ( further reading )
- 在 Gmail 中配置您的发送帐户,以
访问不太安全的应用
- 根据以下 support page 有一个打开
选项
Allowing less secure apps to access your account Google may block sign-in attempts from some apps or devices that do not use modern security standards. Since these apps and devices are easier to break into, blocking them helps keep your account safe.
Some examples of apps that do not support the latest security standards include:
The Mail app on your iPhone or iPad with iOS 6 or below
The Mail app on your Windows phone preceding the 8.1 release
Some Desktop mail clients like Microsoft Outlook and Mozilla Thunderbird
...
Option 2: Change your settings to allow less secure apps to access your account. We don't recommend this option because it might make it easier for someone to break into your account. If you want to allow access anyway, follow these steps:
Go to the "Less secure apps" section in My Account.
Next to "Access for less secure apps," select Turn on. (Note to Google Apps users: This setting is hidden if your administrator has locked less secure app account access.)
CDO现在已经很旧了,所以假设这是一个不支持最新安全标准的应用示例。
关于vba - 通过 SMTP 发送邮件时,我收到 "Transport Failed to Connect to server",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41463936/