ポート番号を指定してSMTPでメールを送信するSystem.Web.Mail.SmtpMailクラスでメールを送信する場合、通常はポート番号として25が使用されます。ここではポート番号を指定してメールを送る方法を紹介します。 .NET Framework 2.0以降で、SmtpClientクラスを使う方法SmtpClientクラスでは、Portプロパティを設定するだけです。 [VB.NET] 'MailMessageの作成 Dim msg As New System.Net.Mail.MailMessage( _ "from@xxx.xxx", "to@xxx.xxx", "題名", "本文") Dim sc As New System.Net.Mail.SmtpClient() 'SMTPサーバーを指定する sc.Host = "localhost" 'ポート番号を指定する sc.Port = 25 'メッセージを送信する sc.Send(msg) '後始末 msg.Dispose() [C#] //MailMessageの作成 System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage( "from@xxx.xxx", "to@xxx.xxx", "題名", "本文"); System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient(); //SMTPサーバーを指定する sc.Host = "localhost"; //ポート番号を指定する sc.Port = 25; //メッセージを送信する sc.Send(msg); //後始末 msg.Dispose(); 補足:ポート番号のデフォルト値をアプリケーション構成ファイルに記述しておくこともできます。詳しくは、こちらをご覧ください。 MailMessage.Fieldsプロパティを使う方法.NET Framework 1.1からは、MailMessageクラスにFieldsプロパティが加わり、これを使うことにより、CDOのフィールドにアクセスできるようになりました。これを使ってCDOのsmtpserverportフィールドを変更すれば、System.Web.Mail.SmtpMailクラスでもポート番号を指定してメールを送信することができます。 [VB.NET] Dim mail As New System.Web.Mail.MailMessage 'From mail.From = "xxx@xxx.xxx" 'To mail.To = "xxx@xxx.xxx" 'Subject mail.Subject = "テスト" '本文 mail.Body = "これはテストです。" 'ポート番号を指定する mail.Fields.Item( _ "http://schemas.microsoft.com/cdo/configuration/smtpserverport") _ = 25 'SMTPサーバーを指定 System.Web.Mail.SmtpMail.SmtpServer = "xxx.xxx.xxx" 'メールを送信 System.Web.Mail.SmtpMail.Send(mail) [C#] System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage(); //From mail.From = "xxx@xxx.xxx"; //To mail.To = "xxx@xxx.xxx"; //Subject mail.Subject = "テスト"; //本文 mail.Body = "これはテストです。"; //ポート番号を指定する mail.Fields[ "http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = 25; System.Web.Mail.SmtpMail.SmtpServer = "xxx.xxx.xxx"; //メールを送信 System.Web.Mail.SmtpMail.Send(mail);
CDOを使う方法.NET Framework 1.0を使用している場合は、残念ながらこのようにMailMessageクラスのFieldsプロパティを使うことが出来ません。しかし、.NET Framework 1.0でもCDOを使用することは可能です。次にCDOを使ってメールを送信するサンプルを紹介しましょう。遅延バインディングを行っているため、ちょっと複雑になっています。 [VB.NET] Dim t As Type = Type.GetTypeFromProgID("CDO.Message") Dim cdo As Object = Activator.CreateInstance(t) t.InvokeMember("From", _ System.Reflection.BindingFlags.SetProperty, _ Nothing, cdo, New Object() {"xxx@xxx.xxx"}) t.InvokeMember("To", _ System.Reflection.BindingFlags.SetProperty, _ Nothing, cdo, New Object() {"xxx@xxx.xxx"}) t.InvokeMember("Subject", _ System.Reflection.BindingFlags.SetProperty, _ Nothing, cdo, New Object() {"テスト"}) t.InvokeMember("Textbody", _ System.Reflection.BindingFlags.SetProperty, _ Nothing, cdo, New Object() {"これはテストです!!"}) Dim conf As Object = t.InvokeMember("Configuration", _ System.Reflection.BindingFlags.GetProperty, _ Nothing, cdo, Nothing) Dim fields As Object = t.InvokeMember("Fields", _ System.Reflection.BindingFlags.GetProperty, _ Nothing, conf, Nothing) t.InvokeMember("Item", _ System.Reflection.BindingFlags.SetProperty, _ Nothing, fields, New Object() { _ "http://schemas.microsoft.com/cdo/configuration/sendusing", _ 2}) 'SMTPサーバーを指定する t.InvokeMember("Item", _ System.Reflection.BindingFlags.SetProperty, _ Nothing, fields, New Object() { _ "http://schemas.microsoft.com/cdo/configuration/smtpserver", _ "xxx.xxx.xxx"}) 'ポート番号を指定する t.InvokeMember("Item", _ System.Reflection.BindingFlags.SetProperty, _ Nothing, fields, New Object() { _ "http://schemas.microsoft.com/cdo/configuration/smtpserverport", _ 25}) t.InvokeMember("Update", _ System.Reflection.BindingFlags.InvokeMethod, _ Nothing, fields, New Object() {}) '送信する t.InvokeMember("Send", _ System.Reflection.BindingFlags.InvokeMethod, _ Nothing, cdo, New Object() {})
[C#]
Type t = Type.GetTypeFromProgID("CDO.Message");
object cdo = Activator.CreateInstance(t);
t.InvokeMember("From",
System.Reflection.BindingFlags.SetProperty,
null, cdo, new object[] {"xxx@xxx.xxx"});
t.InvokeMember("To",
System.Reflection.BindingFlags.SetProperty,
null, cdo, new object[] {"xxx@xxx.xxx"});
t.InvokeMember("Subject",
System.Reflection.BindingFlags.SetProperty,
null, cdo, new object[] {"テスト"});
t.InvokeMember("Textbody",
System.Reflection.BindingFlags.SetProperty,
null, cdo, new object[] {"これはテストです!!"});
object conf = t.InvokeMember("Configuration",
System.Reflection.BindingFlags.GetProperty,
null, cdo, null);
object fields = t.InvokeMember("Fields",
System.Reflection.BindingFlags.GetProperty,
null, conf, null);
t.InvokeMember("Item",
System.Reflection.BindingFlags.SetProperty,
null, fields,
new object[] {
"http://schemas.microsoft.com/cdo/configuration/sendusing",
2});
//SMTPサーバーを指定する
t.InvokeMember("Item",
System.Reflection.BindingFlags.SetProperty,
null, fields,
new object[] {
"http://schemas.microsoft.com/cdo/configuration/smtpserver",
"xxx.xxx.xxx"});
//ポート番号を指定する
t.InvokeMember("Item",
System.Reflection.BindingFlags.SetProperty,
null, fields,
new object[] {
"http://schemas.microsoft.com/cdo/configuration/smtpserverport",
25});
t.InvokeMember("Update",
System.Reflection.BindingFlags.InvokeMethod,
null, fields,
new object[] {});
//送信する
t.InvokeMember("Send",
System.Reflection.BindingFlags.InvokeMethod,
null, cdo,
new object[] {});
それ以外の方法CDOを使えない、あるいは使いたくないという場合は、自分でコードを書くか、誰かが作ったコンポーネントやクラスを使わせてもらうかということになるでしょう。例えば、Tatsuo Babaさんの「BASP21 DLL」を使わせていただくのも有効な方法の一つです。 自分でコードを書くということになると、ソケットを使用することになります。ソケットを使ってSMTPでメールを送信するサンプルは、「SMTP認証でメールを送信する」で示しますので、そちらをご覧ください。
(この記事は、「.NETプログラミング研究 第52号」で紹介したものを基にしています。) 注意:この記事では、基本的な事柄の説明が省略されているかもしれません。初心者の方は、特に以下の点にご注意ください。
|
|
Copyright 2002-2008 DOBON!. All rights reserved.
|