元の KB 番号: 310212
概要
この記事では、Windows 2000 ライブラリ (Cdosys.dll) のコラボレーション データ オブジェクト (CDO) を使用して、添付ファイルを含む電子メール メッセージを送信する方法について説明します。 ローカル SMTP サーバーを使用するか、Microsoft Visual C# のスマート ホスト サーバーを使用して、電子メール メッセージの本文でテキストまたは HTML または Web ページを送信できます。
注
Cdosys.dll ライブラリは CDOSYS とも呼ばれます。
詳細情報
概要セクションの説明に従って CDOSYS を使用するには、次の手順に従います。
Microsoft Visual Studio を起動します。
[ファイル] メニューの [新しい ]選択し、[プロジェクト 選択します。
[プロジェクトの種類]で、Visual C# 選択し、[テンプレート]コンソール アプリケーション を選択します。 既定では、Program.csが作成されます。
注
Microsoft Visual C# .NET 2003 では、Visual C# が Visual C# プロジェクト に変更されます。 既定では、Class1.csが作成されます。
Microsoft CDO for Windows 2000 Libraryへの参照を追加します。 これを行うには、次の手順に従います。
[プロジェクト] メニューの [参照の追加] を選択します。
[COM] タブで、Microsoft CDO for Windows 2000 Library 見つけます。
注
Visual C# .NET 2003 で、[の選択]選択します。
選択を受け入れるには、[参照 の追加] ダイアログ ボックスで [OK] 選択します。
選択したライブラリのラッパーを生成するダイアログ ボックスが表示された場合は、[はい] 選択します。
コード ウィンドウで、すべてのコードを次のコードに置き換えます。
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; } } }
TODO がコードに表示される場合は、示されているようにコードを変更します。
プログラムをビルドして実行するには、F5 キーを押します。
電子メール メッセージが送受信されたことを確認します。
参考資料
Visual Studio を使用した Microsoft Office 開発の詳細については、「Microsoft Office Development with Visual Studio」を参照してください。