CC、BCC、添付ファイル、優先順位などを指定してメールを送信する
注意:以下のサンプルコードの送信者と送信先、さらにCC、BCCは必ず変更するようにしてください。
「SMTPでメールを送信する」を拡張します。CC、BCCの複数指定、添付ファイル指定、HTMLメールとして送る、優先順位指定などの方法が追加されています。 .NET Framework 2.0以降で、SmtpClientクラスを使用する方法以下に具体的なコードを示します。ここでは、CC、BCC、ReplyTo、Sender、優先順位、添付ファイル、配達の通知を設定しています。 'MailMessageの作成 Dim msg As New System.Net.Mail.MailMessage() '送信者 msg.From = New System.Net.Mail.MailAddress("sender@xxx.xxx") '宛先 msg.To.Add(New System.Net.Mail.MailAddress("recipient@xxx.xxx")) 'あて先をもう一人追加 msg.To.Add(New System.Net.Mail.MailAddress("sato@xxx.xxx")) 'CC msg.CC.Add(New System.Net.Mail.MailAddress("cc@xxx.xxx")) 'BCC msg.Bcc.Add(New System.Net.Mail.MailAddress("bcc@xxx.xxx")) 'ReplyTo msg.ReplyTo = New System.Net.Mail.MailAddress("replyto@xxx.xxx") 'Sender msg.Sender = New System.Net.Mail.MailAddress("master@xxx.xxx") '件名 msg.Subject = "こんにちは" '本文 msg.Body = "こんにちは。" + vbCrLf + vbCrLf + "それではまた。" '優先順位を「重要」にする msg.Priority = System.Net.Mail.MailPriority.High 'メールの配達が遅れたとき、失敗したとき、正常に配達されたときに通知する msg.DeliveryNotificationOptions = _ System.Net.Mail.DeliveryNotificationOptions.Delay Or _ System.Net.Mail.DeliveryNotificationOptions.OnFailure Or _ System.Net.Mail.DeliveryNotificationOptions.OnSuccess '"C:\test1.png"を添付する Dim attach1 As New System.Net.Mail.Attachment("C:\test1.gif") msg.Attachments.Add(attach1) 'さらに、"C:\test2.png"も添付する Dim attach2 As New System.Net.Mail.Attachment("C:\test2.gif") msg.Attachments.Add(attach2) Dim sc As New System.Net.Mail.SmtpClient() 'SMTPサーバーを指定する sc.Host = "localhost" 'メッセージを送信する sc.Send(msg) '後始末 msg.Dispose() //MailMessageの作成 System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); //送信者 msg.From = new System.Net.Mail.MailAddress("sender@xxx.xxx"); //宛先 msg.To.Add(new System.Net.Mail.MailAddress("recipient@xxx.xxx")); //あて先をもう一人追加 msg.To.Add(new System.Net.Mail.MailAddress("sato@xxx.xxx")); //CC msg.CC.Add(new System.Net.Mail.MailAddress("cc@xxx.xxx")); //BCC msg.Bcc.Add(new System.Net.Mail.MailAddress("bcc@xxx.xxx")); //ReplyTo msg.ReplyTo = new System.Net.Mail.MailAddress("replyto@xxx.xxx"); //Sender msg.Sender = new System.Net.Mail.MailAddress("master@xxx.xxx"); //件名 msg.Subject = "こんにちは"; //本文 msg.Body = "こんにちは。\r\n\r\nそれではまた。"; //優先順位を「重要」にする msg.Priority = System.Net.Mail.MailPriority.High; //メールの配達が遅れたとき、失敗したとき、正常に配達されたときに通知する msg.DeliveryNotificationOptions = System.Net.Mail.DeliveryNotificationOptions.Delay | System.Net.Mail.DeliveryNotificationOptions.OnFailure | System.Net.Mail.DeliveryNotificationOptions.OnSuccess; //"C:\test1.png"を添付する System.Net.Mail.Attachment attach1 = new System.Net.Mail.Attachment("C:\\test1.gif"); msg.Attachments.Add(attach1); //さらに、"C:\test2.png"も添付する System.Net.Mail.Attachment attach2 = new System.Net.Mail.Attachment("C:\\test2.gif"); msg.Attachments.Add(attach2); System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient(); //SMTPサーバーを指定する sc.Host = "localhost"; //メッセージを送信する sc.Send(msg); //後始末 msg.Dispose(); 上記のコードを実行すると、次のようなデータがサーバーに送信されます。なお、添付ファイルのデータ部分は、省略しています。 DeliveryNotificationOptionsプロパティについてDeliveryNotificationOptionsプロパティは、RCPTのオプションである「RCPT TO:<メールアドレス> NOTIFY=SUCCESS,DELAY,FAILURE」をつけるための設定です。これにより、メールの配信通知(Delivery Status Notification)を受け取ることができます。ただし、サーバーが対応している必要があります。 なお、「フィードバック: SMTPClient Not setting DeliveryNotificationOptions for each message sent」によると、現在この機能は正常に機能しないようです。複数のメールを送信すると、はじめのメールだけが有効になるようです。 SmtpMailクラスを使用する方法以下に具体的なコードを示します。 Dim mm As New System.Web.Mail.MailMessage() Dim attachment As System.Web.Mail.MailAttachment '送信者 mm.From = "sender <sender@xxx.xx.com>" 'あて先 mm.To = "recipient1 <recipient1@xxx.xx.com>" 'CC(複数指定するときはセミコロンで区切る) mm.Cc = "cc1 <cc1@xxx.xx.com>; cc2 <cc2@xxx.xx.com>; cc3@xxx.xx.com" 'BCC(複数指定するときはセミコロンで区切る) mm.Bcc = "bcc1@xxx.xx.com; bcc2@xxx.xx.com" '件名 mm.Subject = "テスト" '本文 mm.Body = "こんにちは。これはテストです。" '本文の文字コードを指定する(ここではJIS) mm.BodyEncoding = System.Text.Encoding.GetEncoding(50220) '添付ファイルの指定(UUEncodeでエンコードされる) attachment = New System.Web.Mail.MailAttachment("test1.jpg") 'Base64でエンコードするときは次のようにする 'attachment = New System.Web.Mail.MailAttachment("test1.jpg", _ ' Web.Mail.MailEncoding.Base64) 'Attachmentsに追加する mm.Attachments.Add(attachment) '同様にして複数の添付ファイルを追加できる 'HTMLメールとして送るときは次のようにする 'mm.BodyFormat = Web.Mail.MailFormat.Html 'メールの優先順位を指定する(ここでは優先順位を高くする) mm.Priority = Web.Mail.MailPriority.High 'SMTPサーバーを指定する System.Web.Mail.SmtpMail.SmtpServer = "(SMTPサーバーを指定する)" '送信する System.Web.Mail.SmtpMail.Send(mm) System.Web.Mail.MailMessage mm = new System.Web.Mail.MailMessage(); System.Web.Mail.MailAttachment attachment; //送信者 mm.From = "sender <sender@xxx.xx.com>"; //あて先 mm.To = "recipient1 <recipient1@xxx.xx.com>"; //CC(複数指定するときはセミコロンで区切る) mm.Cc = "cc1 <cc1@xxx.xx.com>; cc2 <cc2@xxx.xx.com>; cc3@xxx.xx.com"; //BCC(複数指定するときはセミコロンで区切る) mm.Bcc = "bcc1@xxx.xx.com; bcc2@xxx.xx.com"; //件名 mm.Subject = "テスト"; //本文 mm.Body = "こんにちは。これはテストです。"; //本文の文字コードを指定する(ここではJIS) mm.BodyEncoding = System.Text.Encoding.GetEncoding(50220); //添付ファイルの指定(UUEncodeでエンコードされる) attachment = new System.Web.Mail.MailAttachment("test1.jpg"); //Base64でエンコードするときは次のようにする //attachment = new System.Web.Mail.MailAttachment("test1.jpg", // System.Web.Mail.MailEncoding.Base64); //Attachmentsに追加する mm.Attachments.Add(attachment); //同様にして複数の添付ファイルを追加できる //HTMLメールとして送るときは次のようにする //mm.BodyFormat = System.Web.Mail.MailFormat.Html; //メールの優先順位を指定する(ここでは優先順位を高くする) mm.Priority = System.Web.Mail.MailPriority.High; //SMTPサーバーを指定する System.Web.Mail.SmtpMail.SmtpServer = "(SMTPサーバーを指定する)"; //送信する System.Web.Mail.SmtpMail.Send(mm); 複数のファイルを添付する複数のファイルを添付する方法をもうすこし詳しく説明しましょう。複数の添付ファイルを送信するには、送信する添付ファイルごとにMailAttachmentオブジェクトを作成し、そのすべてをMailMessage.Attachmentsプロパティのコレクションに追加します。 次の例では、ファイル"test1.jpg"と"test2.jpg"の2つのファイルを添付ファイルとしたメールを送信しています。 Dim mm As New System.Web.Mail.MailMessage() Dim attachment As System.Web.Mail.MailAttachment '送信者 mm.From = "sender <sender@xxx.xx.com>" 'あて先 mm.To = "recipient1 <recipient1@xxx.xx.com>" '件名 mm.Subject = "テスト" '本文 mm.Body = "こんにちは。これはテストです。" '本文の文字コードを指定する(ここではJIS) mm.BodyEncoding = System.Text.Encoding.GetEncoding(50220) '添付ファイルの指定 attachment = New System.Web.Mail.MailAttachment("test1.jpg") 'Attachmentsに追加する mm.Attachments.Add(attachment) 'さらに添付ファイルを追加する attachment = New System.Web.Mail.MailAttachment("test2.jpg") mm.Attachments.Add(attachment) 'SMTPサーバーを指定する System.Web.Mail.SmtpMail.SmtpServer = "localhost" '送信する System.Web.Mail.SmtpMail.Send(mm) System.Web.Mail.MailMessage mm = new System.Web.Mail.MailMessage(); System.Web.Mail.MailAttachment attachment; //送信者 mm.From = "sender <sender@xxx.xx.com>"; //あて先 mm.To = "recipient1 <recipient1@xxx.xx.com>"; //件名 mm.Subject = "テスト"; //本文 mm.Body = "こんにちは。これはテストです。"; //本文の文字コードを指定する(ここではJIS) mm.BodyEncoding = System.Text.Encoding.GetEncoding(50220); //添付ファイルの指定 attachment = new System.Web.Mail.MailAttachment("test1.jpg"); //Attachmentsに追加する mm.Attachments.Add(attachment); //さらに添付ファイルを追加する attachment = new System.Web.Mail.MailAttachment("test2.jpg"); mm.Attachments.Add(attachment); //SMTPサーバーを指定する System.Web.Mail.SmtpMail.SmtpServer = "localhost"; //送信する System.Web.Mail.SmtpMail.Send(mm);
注意:この記事では、基本的な事柄の説明が省略されているかもしれません。初心者の方は、特に以下の点にご注意ください。 |
|
Copyright(C) DOBON!. All rights reserved.
|