gpt4 book ai didi

excel - VBA Excel-从Excel发送邮件

转载 作者:行者123 更新时间:2023-12-02 18:40:49 25 4
gpt4 key购买 nike

我有以下代码行可以在命令按钮单击事件下发送邮件。

Private Sub CommandButton1_Click()
Dim cdoConfig
Dim msgOne

Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServerPort) = 557
.Item(cdoSMTPServer) = "smtp.emailsr.com" 'SMTP server goes here
'.Item(cdoSendUserName) = "My Username"
'.Item(cdoSendPassword) = "myPassword"
.Update
End With

Set msgOne = CreateObject("CDO.Message")
Set msgOne.Configuration = cdoConfig
msgOne.To = "adbc@adbc.com"
msgOne.from = "bcda@adbc.com"
msgOne.Subject = "Test CDO"
msgOne.TextBody = "It works just fine."
msgOne.Send
End Sub

当我执行此操作时,我遇到类似运行时错误-2147220977(8004020f):自动化错误此订阅的事件类位于无效分区的错误

msgOne.Send

上面的行在执行过程中给出了错误。因此,我转向使用 CDO 方法发送电子邮件。现在我正在执行以下代码。

Dim iMsg As Object
Dim iConf As Object
Dim strbody As String
Dim Flds As Variant

Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")

iConf.Load -1 ' CDO Source Defaults
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mysmtpserver.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "mymailId"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Mypassword"
.Update

End With

strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"

With iMsg
Set .Configuration = iConf
.To = "tomailid"
.CC = ""
.BCC = ""
.From = "mymailid"
.Subject = "New"
.TextBody = strbody
.Send
End With

但是发送给我一个错误,例如运行时错误-2147220977(8004020f):服务器拒绝了一个或多个收件人地址。服务器响应是:554 5.7.1:发件人地址被拒绝:访问被拒绝有时它就像运行时错误-'2147220975(80040211)自动化错误

最佳答案

如果您注册了 CDO 类型库,您正在使用的代码可以在 VBScript 或其他类似语言中运行。类型库包含属性 cdoSendUsingMethod 等,因此您不必使用完整的 urn。在VBA中,你必须使用完整的瓮。 Ron De Bruin 在 http://www.rondebruin.nl/cdo.htm 对此有一个很好的引用。 .

从他的网站上,您可以看到您的代码与 VBA 所需的代码之间的差异,特别是在这里:

     Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") _
= "Fill in your SMTP server here"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Update
End With

关于excel - VBA Excel-从Excel发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14250764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com