问题描述
我有下面的代码使用CDO从VBA宏发送邮件.我在代码中收到错误:
I have the code below to send mail from a VBA macro using CDO. I get an error in the code:
我正在从Gmail SMTP服务发送邮件.看起来配置已正确设置,但是以某种方式无法正常工作.
I am sending a mail from the Gmail SMTP service. Looks like the configuration is set correctly, but somehow it doesn't work.
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
推荐答案
该代码对我来说很好用(将从 Gmail 发送到 Gmail)-因此,您需要检查以下内容:
That code works totally fine for me (send from Gmail to Gmail) - so you need to check the following:
- 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
一些不支持最新安全标准的应用程序示例包括:
Some examples of apps that do not support the latest security standards include:
在装有iOS 6或更低版本的iPhone或iPad上的Mail应用
The Mail app on your iPhone or iPad with iOS 6 or below
8.1之前版本的Windows手机上的Mail应用
The Mail app on your Windows phone preceding the 8.1 release
某些桌面邮件客户端,例如Microsoft Outlook和Mozilla Thunderbird
Some Desktop mail clients like Microsoft Outlook and Mozilla Thunderbird
...
选项2:更改设置,以允许安全性较差的应用访问您的帐户.我们不建议您使用此选项,因为它可能使某人更容易进入您的帐户.如果仍然要允许访问,请按照以下步骤操作:
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:
转到我的帐户"中的安全应用程序较少"部分.
在访问不太安全的应用程序"旁边,选择打开.(对Google Apps用户的注意:如果您的管理员锁定了不太安全的应用程序帐户访问权限,则此设置将隐藏.)
CDO 很老了现在,假设这是不支持最新安全标准的应用示例.
CDO is pretty old now so assume that is an example of an app that doesn't support latest security standards.
这篇关于通过SMTP发送邮件时,我收到“传输无法连接到服务器"消息.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!