我有以下代码行在命令按钮单击事件下发送邮件。
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
当我执行此操作时,我遇到了类似RunTime Error-2147220977(8004020f): Automation Error 此订阅的事件类位于无效分区中的错误
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)自动化错误