【发布时间】:2023-03-11 21:01:01
【问题描述】:
我正在尝试使用 Microsoft Office Excel 2007 VBA 代码发送邮件,但出现错误:
运行时错误'-2147220973 (80040213)':
自动化错误
我使用的代码是:
Dim cdomsg As Object
Set cdomsg = CreateObject("CDO.message")
With cdomsg.Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 25
.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") = "excel.**********@gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "**********123"
' .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Update
End With
With cdomsg
.Subject = "Automated mail"
.From = "excel.**********@gmail.com"
.To = "**********@hitbts.com" ' https://temp-mail.org/
.TextBody = "Automated mail"
.AddAttachment ("*:\*****\***********\****************\***********\*****\*****.xlsm")
.Send
End With
Set cdomsg = Nothing
我尝试过其他 smpt 服务器,输入nslookup 时在 cmd 中显示的服务器名称和地址,计算机的 IP 和另一个 IP 但我不知道正确的 smpt 服务器是什么。
回答后编辑:
对于将来搜索此内容的任何人,我使用和工作的代码如下(取自 this 视频):
Dim Mail As New Message
Dim Config As Configuration
Set Config = Mail.Configuration
Config(cdoSendUsingMethod) = cdoSendUsingPort
Config(cdoSMTPServer) = "smtp.gmail.com"
Config(cdoSMTPServerPort) = 25
Config(cdoSMTPAuthenticate) = cdoBasic
Config(cdoSMTPUseSSL) = True
Config(cdoSendUserName) = "sender@gmail.com"
Config(cdoSendPassword) = "password123"
Config.Fields.Update
Mail.AddAttachment ("C:\path\file.ext")
Mail.To = "destination@gmail.com"
Mail.From = Config(cdoSendUserName)
Mail.Subject = "Email Subject"
Mail.HTMLBody = "<b>Email Body</b>"
Mail.Send
确保更改 "sender@gmail.com"、"password123"、"C:\path\file.ext" 和 "destination@gmail.com" 以使示例正常工作,并更改邮件的主题和正文。
我还转到 VBA 上的顶部菜单“工具”,选项“参考...”,启用“Microsoft CDO for Windows 2000 库”并按下确定,如上面链接的视频所示。
Direct link 为来自here 的 GMail 启用“不太安全”选项。
【问题讨论】:
-
查看this 以获得良好的工作 CDO.Mail 代码。
-
我在以下行中看到一个错误:
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 25。 smptserverport 应该是 smtpserverport -
@Slaqr 你是对的,但这仍然没有解决错误。
-
当您使用 Gmail 时;您是否检查过启用“不太安全的应用程序”是否有所作为? link
-
@Slaqr 问题已解决,邮件已发送,非常感谢!如果您可以将其设为offial answer,则可以接受。
标签: vba excel cdo.message