public void SendMail()
{
Encoding encode = Encoding.GetEncoding("UTF-8");
MailMessage myMail = new MailMessage();
myMail.From = new MailAddress("admin@test.com", string.Empty, encode);
myMail.To.Add("a@test.com, b@test.com");
myMail.Subject = "Test";
myMail.SubjectEncoding = encode;
myMail.Body = "<img src=\"test1.jpg\" /><img src=\"test2.jpg\" />";
myMail.BodyEncoding = encode;
//需啟用html格式才可內嵌圖片
myMail.IsBodyHtml = true;
myMail.Attachments.Add(GetAttachment("test1.jpg", encode));
myMail.Attachments.Add(GetAttachment("test2.jpg", encode));
SmtpClient smtp = new SmtpClient("localhost");
try
{
smtp.Send(myMail);
MessageBox.Show(DateTime.Now.ToString() + " 寄信成功");
}
catch (Exception ex)
{
MessageBox.Show(DateTime.Now.ToString() + " 寄信失敗" + ex.ToString());
}
}
private Attachment GetAttachment(string strFileName, Encoding encode)
{
//設定圖片路徑
string strFilePath = @"C:\" + strFileName;
Attachment attachment = new Attachment(strFilePath);
attachment.Name = strFileName;
attachment.NameEncoding = encode;
attachment.TransferEncoding = TransferEncoding.Base64;
// 設定附件為內嵌
attachment.ContentDisposition.Inline = true;
attachment.ContentDisposition.DispositionType = "inline";
return attachment;
}
參考資料:
http://blog.miniasp.com/post/2008/02/06/How-to-send-Email-with-embedded-picture-image.aspx
http://www.dotblogs.com.tw/sam19820611/archive/2009/09/09/10509.aspx
沒有留言:
張貼留言