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

これを実行すると、ランタイムエラー-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)のようなエラーが発生します。サーバーが1つ以上の受信者アドレスを拒否しました。サーバーの応答は次のとおりです。5545.7.1:送信者アドレスが拒否されました:アクセスが拒否されました。ランタイムエラーのようになることもあります-'2147220975(80040211)自動化エラー

4

1 に答える 1

3

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
于 2013-01-10T04:56:21.090 に答える