如何发送内嵌图片的 E-mail ( Inline Attachment )
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
namespace SystemNetMailTest
{
class Program
{
static void Main(string[] args)
{
string myMailEncoding = "utf-8";
string myFromEmail = "testtest.com";
string myFromName = "測試寄件者";
string myToEmail = "testtest.com";
string myToName = "測試收件者";
MailAddress from =
new MailAddress(myFromEmail, myFromName,
Encoding.GetEncoding(myMailEncoding));
MailAddress to =
new MailAddress(myToEmail, myToName,
Encoding.GetEncoding(myMailEncoding));
MailMessage myMessage = new MailMessage(from, to);
myMessage.Subject = "郵件主旨";
myMessage.SubjectEncoding = Encoding.GetEncoding(myMailEncoding);
myMessage.Body = "<h1>這是郵件內容</h1><hr/><img src=\"Logo.gif\" />";
myMessage.BodyEncoding = Encoding.GetEncoding(myMailEncoding);
myMessage.IsBodyHtml = true;
myMessage.Priority = MailPriority.High;
// 設定附件檔案(Attachment)
string strFilePath = @"C:\Logo.gif";
System.Net.Mail.Attachment attachment1 =
new System.Net.Mail.Attachment(strFilePath);
attachment1.Name = System.IO.Path.GetFileName(strFilePath);
attachment1.NameEncoding = Encoding.GetEncoding(myMailEncoding);
attachment1.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
// 設定該附件為一個內嵌附件(Inline Attachment)
attachment1.ContentDisposition.Inline = true;
attachment1.ContentDisposition.DispositionType =
System.Net.Mime.DispositionTypeNames.Inline;
myMessage.Attachments.Add(attachment1);
SmtpClient smtp = new SmtpClient("localhost");
try
{
smtp.Send(myMessage);
Console.WriteLine(DateTime.Now.ToString() + " 寄信成功");
}
catch
{
Console.WriteLine(DateTime.Now.ToString() + " 寄信失敗!!!");
}
Console.ReadKey();
}
}
}