Cdosys.dll ライブラリを使用して、添付ファイルを含む電子メール メッセージを送信する方法

元の KB 番号: 310212

概要

この記事では、Windows 2000 ライブラリ (Cdosys.dll) 用コラボレーション データ オブジェクト (CDO) を使用して、添付ファイルを含む電子メール メッセージを送信する方法について説明します。 ローカル SMTP サーバーを使用するか、Microsoft Visual C# のスマート ホスト サーバーを使用して、電子メール メッセージの本文にテキストまたは HTML または Web ページを送信できます。

注:

Cdosys.dll ライブラリは CDOSYS とも呼ばれます。

詳細

「概要」セクションの説明に従って CDOSYS を使用するには、次の手順に従います。

  1. Microsoft Visual Studio を起動します。

  2. [ ファイル ] メニューの [ 新規] を選択し、[ プロジェクト] を選択します。

  3. [プロジェクトの種類] で [Visual C#] を選択し、[テンプレート] の [コンソール アプリケーション] を選択します。 既定では、Program.csが作成されます。

    注:

    Microsoft Visual C# .NET 2003 では、 Visual C#Visual C# プロジェクトに変更されます。 既定では、Class1.csが作成されます。

  4. Microsoft CDO For Windows 2000 ライブラリへの参照を追加します。 これを行うには、次の手順を実行します。

    1. [ プロジェクト ] メニューの [ 参照の追加] を選択します。

    2. [ COM ] タブで、 Microsoft CDO For Windows 2000 ライブラリを見つけます。

      注:

      Visual C# .NET 2003 で、[選択] を 選択します

    3. 選択を受け入れるには、[参照の追加] ダイアログ ボックスで [OK] を選択します

      選択したライブラリのラッパーを生成するダイアログ ボックスが表示された場合は、[ はい] を選択します。

  5. コード ウィンドウで、すべてのコードを次のコードに置き換えます。

    namespace CdoSys {
        using System;
        class Class1 {
            static void Main (string[] args) {
                try {
                    CDO.Message oMsg = new CDO.Message ();
                    CDO.IConfiguration iConfg;
    
                    iConfg = oMsg.Configuration;
    
                    ADODB.Fields oFields;
                    oFields = iConfg.Fields;
    
                    // Set configuration.
                    ADODB.Field oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
    
                    //TODO: To send by using the smart host, uncomment the following lines:
                    //oField.Value = CDO.CdoSendUsing.cdoSendUsingPort;
                    //oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"];
                    //oField.Value = "smarthost";
    
                    // TODO: To send by using local SMTP service.
                    //oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
                    //oField.Value = 1;
    
                    oFields.Update ();
    
                    // Set common properties from message.
    
                    //TODO: To send text body, uncomment the following line:
                    //oMsg.TextBody = "Hello, how are you doing?";
    
                    //TODO: To send HTML body, uncomment the following lines:
                    //String sHtml;
                    //sHtml = "<HTML>\n" +
                    //"<HEAD>\n" +
                    //"<TITLE>Sample GIF</TITLE>\n" +
                    //"</HEAD>\n" +
                    //"<BODY><P>\n" +
                    //"<h1><Font Color=Green>Inline graphics</Font></h1>\n" +
                    //"</BODY>\n" +
                    //"</HTML>";
                    //oMsg.HTMLBody = sHtml;
    
                    //TOTO: To send WEb page in an e-mail, uncomment the following lines and make changes in TODO section.
                    //TODO: Replace with your preferred Web page
                    //oMsg.CreateMHTMLBody("http://www.microsoft.com",
                    //CDO.CdoMHTMLFlags.cdoSuppressNone,
                    //"", "");
                    oMsg.Subject = "Test SMTP";
    
                    //TODO: Change the To and From address to reflect your information.
                    oMsg.From = "someone@example.com";
                    oMsg.To = "someone@example.com";
                    //ADD attachment.
                    //TODO: Change the path to the file that you want to attach.
                    oMsg.AddAttachment ("C:\\Hello.txt", "", "");
                    oMsg.AddAttachment ("C:\\Test.doc", "", "");
                    oMsg.Send ();
                } catch (Exception e) {
                    Console.WriteLine ("{0} Exception caught.", e);
                }
                return;
            }
        }
    }
    
  6. TODO がコードに表示される場合は、示されているようにコードを変更します。

  7. プログラムをビルドして実行するには、F5 キーを押します。

  8. 電子メール メッセージが送受信されていることを確認します。

関連情報

Visual Studio を使用した Microsoft Office 開発の詳細については、「Microsoft Office Development with Visual Studio」を参照してください。