3

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

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)自动化错误

4

1 回答 1

3

如果您注册了 CDO 类型库,您正在使用的代码可以在 VBScript 或其他类似语言中运行。类型库包含属性cdoSendUsingMethod等,因此您不必使用完整的骨灰盒。在 VBA 中,您必须使用完整的 urn。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
于 2013-01-10T04:56:21.090 回答