■■■

2016年4月4日月曜日

SmtpClientで添付ファイルを送信する方法

SmtpClientで添付ファイルを送信する方法
VB.NET
Dim msg As New System.Net.Mail.MailMessage()
msg.From = New System.Net.Mail.MailAddress("genisys@xxx.xxx")
msg.To.Add(New System.Net.Mail.MailAddress("do@xxx.xxx"))
msg.To.Add(New System.Net.Mail.MailAddress("d.ogenisys@xxx.xxx"))
msg.CC.Add(New System.Net.Mail.MailAddress("cc@xxx.xxx"))
msg.Bcc.Add(New System.Net.Mail.MailAddress("bcc@xxx.xxx"))
msg.ReplyToList.Add(New System.Net.Mail.MailAddress("replyto@xxx.xxx"))
msg.Sender = New System.Net.Mail.MailAddress("master@xxx.xxx")

msg.Subject = "D,O Genisys"
msg.Body = "
D,O Genisys。" + 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

'添付ファイル
Dim attach1 As New System.Net.Mail.Attachment("C:\test\1.gif")
msg.Attachments.Add(attach1)
'添付ファイル
Dim attach2 As New System.Net.Mail.Attachment("C:\test\2.gif")
msg.Attachments.Add(attach2)

Dim sc As New System.Net.Mail.SmtpClient()
sc.Host = "localhost"
sc.Port = 25
sc.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network
sc.Send(msg)

msg.Dispose()
sc.Dispose()
C#
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"));
msg.CC.Add(new System.Net.Mail.MailAddress("cc@xxx.xxx"));
msg.Bcc.Add(new System.Net.Mail.MailAddress("bcc@xxx.xxx"));
msg.ReplyToList.Add(new System.Net.Mail.MailAddress("replyto@xxx.xxx"));
msg.Sender = new System.Net.Mail.MailAddress("master@xxx.xxx");

msg.Subject = "
D,O Genisys";
msg.Body = "D,O Genisys。\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;

//"添付ファイル
System.Net.Mail.Attachment attach1 =
new System.Net.Mail.Attachment("C:\\test\\1.gif");
msg.Attachments.Add(attach1);
//添付ファイル
System.Net.Mail.Attachment attach2 =
new System.Net.Mail.Attachment("C:\\test\\2.gif");
msg.Attachments.Add(attach2);

System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient();
sc.Host = "localhost";
sc.Port = 25;
sc.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
sc.Send(msg);

msg.Dispose();
sc.Dispose();
■■■