該方式直接保存為HTML文件,也可以是文本文件,其它格式效果不是很好
MailMessage mmsg = new MailMessage();
mmsg.Subject = "郵件標題";
mmsg.Body = "郵件內容";
mmsg.To.Add("[email protected]");//接收郵箱
byte[] bytes = System.Text.Encoding.Default.GetBytes
(@"<table><tr><td width=150>1234567891234567
</td><td width=80>12345678</td></tr></table>");
MemoryStream ms = new MemoryStream(bytes);
ContentType ct = new ContentType();
//附件文件類型
ct.MediaType = MediaTypeNames.Text.Html;
//附件名稱,可以是其它後綴名
ct.Name = "附件名稱" + DateTime.Now.ToString() + ".html";
mmsg.Attachments.Add(new Attachment(ms, ct));
//SMTP簡單郵件協議
System.Net.Mail.SmtpClient sc
= new System.Net.Mail.SmtpClient();
sc.Host = "127.0.0.1";//主機地址
sc.Port = 25;//端口
//發送郵箱賬號和密碼
sc.Credentials =
new System.Net.NetworkCredential("account", "password");
//發送郵箱
mmsg.From = new MailAddress("[email protected]");
sc.Send(mmsg);
//釋放流資源
ms.Close();
ms.Dispose();