vba 通过 SMTP 发送邮件时,我收到“传输无法连接到服务器”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41463936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When sending mail via SMTP, I get "Transport Failed to Connect to server"
提问by Robin Mackenzie
I have the code below to send mail from a VBA macro using CDO. I get an error in the code:
我有下面的代码可以使用 CDO 从 VBA 宏发送邮件。我在代码中收到一个错误:
Transport failed To connect to server Error
传输失败连接到服务器错误
I am sending a mail from the Gmail SMTP service. Looks like the configuration is set correctly, but somehow it doesn't work.
我正在从 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 = "[email protected]"
strTo = "[email protected]"
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") = "[email protected]"
.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
回答by Robin Mackenzie
That code works totally fine for me (send fromGmail toGmail) - so you need to check the following:
该代码对我来说完全正常(从Gmail发送到Gmail)-因此您需要检查以下内容:
- try it with port 587 as well as port 465 (further reading)
- configure your sending account in Gmail for
Access for less secure apps
- there is aTurn On
option per the following 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.)
允许安全性较低的应用访问您的帐户 Google 可能会阻止来自某些不使用现代安全标准的应用或设备的登录尝试。由于这些应用程序和设备更容易被入侵,因此阻止它们有助于确保您的帐户安全。
一些不支持最新安全标准的应用程序示例包括:
装有 iOS 6 或更低版本的 iPhone 或 iPad 上的邮件应用
8.1 版本之前 Windows 手机上的邮件应用程序
一些桌面邮件客户端,如 Microsoft Outlook 和 Mozilla Thunderbird
...
选项 2:更改您的设置以允许安全性较低的应用访问您的帐户。我们不建议使用此选项,因为它可能会让他人更容易侵入您的帐户。如果您仍然想允许访问,请按照下列步骤操作:
转到“我的帐户”中的“安全性较低的应用程序”部分。
在“访问不太安全的应用程序”旁边,选择打开。(Google Apps 用户注意:如果您的管理员锁定了安全性较低的应用帐户访问权限,则此设置将隐藏。)
CDOis pretty old now so assume that is an example of an app that doesn't support latest security standards.
CDO现在已经很老了,所以假设这是一个不支持最新安全标准的应用程序示例。