我试图用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、计算机的IP和另一个IP时,在cmd中显示的服务器名称和地址,但我不知道正确的smpt服务器是什么。
回答后的编辑:
对于将来搜索这些内容的人来说,我使用和使用的代码如下(摘自这视频):
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上的顶部菜单"Tools“,选项”References.“,启用了”MicrosoftCDOforWindows2000Library“,并按了OK,如上面链接的视频所示。
发布于 2019-04-01 09:32:28
哈罗,
我一直在使用与这里讨论的代码类似的代码。它在许多不同的操作系统和Office/ Excel版本中都非常可靠。它还在不同的国家、不同的互联网连接和提供商中可靠地工作。在最近的一次马耳他之行中,它未能在我和我一起使用的两台不同的计算机上工作,这两台电脑有不同的系统和Office/ Excel版本。我尝试过不同的互联网连接和提供商,但都没有成功。
我治愈了这个问题,所以我分享这个解决方案,以防它对将来路过这里的人有帮助。
简单地说,解决方案是将smptserverport") = 25
改为smptserverport") = 465
(我顺便指出,在我以前类似的编码中,(同时使用我的gmail.com电子邮件地址和我的德国电信、tonline.de、电子邮件地址)编码可以使用25或465个)。(我更倾向于使用25,而不是465,仅仅是因为我看到类似编码中使用的频率更高))
这是完全植入我的解决方案,这对我很好。
我已经改变了我的程序的签名行
Sub PetrasDailyProWay1_COM_Way()
因此,它现在接受“smptserverport”号作为其值。
Sub PetrasDailyProWay1_COM_Way(ByVal SmptySvrPrt)
例程的任何Call
,比如我拥有的这个Call
行
Application.Run Macro:="'" & ThisWorkbook.Path & "\" & "NeuProAktuelleMakros.xlsm'" & "!ProAktuelleMacrosMtsch.PetrasDailyProWay1_COM_Way"
被修改为传递值25,因此:
Application.Run Macro:="'" & ThisWorkbook.Path & "\" & "NeuProAktuelleMakros.xlsm'" & "!ProAktuelleMacrosMtsch.PetrasDailyProWay1_COM_Way" , arg1:="25"
(上面的代码行运行过程Sub PetrasDailyProWay1_COM_Way( )
,在我的示例中,它位于另一个工作簿中,而不是Call
行所在的工作簿中。(如果工作簿"NeuProAktuelleMakros.xlsm“尚未打开,则自动打开该代码行)
现在,我已经在我的例程Sub PetrasDailyProWay1_COM_Way( )
的末尾添加了错误处理,它安排了该例程的重新运行,如果初始运行使用25失败,则使用465。(这个特定的解决方案的另一个优点是,我会自动获得第二次尝试,在最初的编码中,在最初的代码中,它以前在第一次尝试时就不起作用)
这是我编码的前一端:
Rem 3 Do it
.send
MsgBox Prompt:="Done"
End With ' CreateObject("CDO.Message") (Rem 1 Library End =======#
End Sub
以下是现在修改的版本:
Rem 3 Do it initially attempt with 25 , then in Malta as well maybe with 465
On Error GoTo Malta ' Intended to catch a possible predicted error in the next line when running the routine in Malta, or/ and an error in the second attempt at a code run ' if the next line errors, then I scheduule the routine to run again with "smtpserverport") = 465
.send
On Error GoTo 0
MsgBox Prompt:="Done (with " & SmptySvrPrt & ")" ' This will typically give either "Done (with 25)" or else "Done (with 465)" if the routine worked
End With ' CreateObject("CDO.Message") (Rem 1 Library End =======#
Exit Sub ' Normal routine end for no error exceptional errected situation
Malta: ' Intended to catch a predicted error when running the routine in Malta, or/ and an error in the second attempt at a code run
If SmptySvrPrt = "465" Then MsgBox Prompt:="Also did not work with 465 , Oh Poo!": Exit Sub ' case error with attempt with 465
Application.OnTime Now(), "'" & ThisWorkbook.Path & "\" & "NeuProAktuelleMakros.xlsm'" & "!'ProAktuelleMacrosMtsch.PetrasDailyProWay1_COM_Way ""465""'"
' On Error GoTo -1: On Error GoTo 0 ' I do not need this as the End Sub will effectively bring down the errection state
End Sub
我在Application.OnTime
代码行中使用的语法很难搞清楚。(这比我所需要的要复杂得多,但我希望保持格式与我的Call
代码行中使用的格式一致)。
我不知道如何在Application.OnTime
代码行的最后一部分中使用( )
括号中的参数。我也想不出如何使用命名参数来执行代码行,我个人更喜欢这样做。如果我调用了一个不带参数的过程,我就可以用命名的参数来完成这个任务。但是,在使用参数的过程中,就像这里新修改的代码一样,我找不到任何有效的语法。因此,如果有人能告诉我如何在一个工作语法中用类似于这个伪形式的形式(不起作用)来做这一行,那么我会非常感兴趣的。
Application.OnTime EarliestTime:=Now(), Procedure:="'" & ThisWorkbook.Path & "\" & "NeuProAktuelleMakros.xlsm'" & "!'ProAktuelleMacrosMtsch.PetrasDailyProWay1_COM_Way, arg1:=""465""'"
之前曾提到使用465代替25,就像使用其中一种或另一种。我还没有看到任何关于这个“smptserverport”或其他参数究竟是什么的解释,至少在任何形式上我都可以理解。如果任何人有任何明确的解释,这将是一个有趣的补充,在这里,我认为。(链接到任何现有的解释对我来说都没用,因为我想我已经看到它们了。…。…我希望这可能是没有人能清楚地记录下来的事情之一,而且在此期间,没有人能记住这一切是关于什么的。
汤恩库:-)艾伦
https://stackoverflow.com/questions/48020578
复制相似问题