该方式直接保存为HTML文件,也可以是文本文件,其它格式效果不是很好
MailMessage mmsg = new MailMessage();
mmsg.Subject = "邮件标题";
mmsg.Body = "邮件内容";
//加上编码防止中文变乱码
mmsg.BodyEncoding = System.Text.Encoding.UTF8;
mmsg.To.Add("accept@qq.com");//接收邮箱
//加上charset=utf-8防止附件中文变乱码
byte[] bytes = System.Text.Encoding.UTF8.GetBytes
(@"<html><head><meta http-equiv=Content-Type
content='text/html;charset=utf-8'></head>
<body><table><tr><td width=150>1234567891234567
</td><td width=80>12345678</td></tr>
</table></body></html>");
MemoryStream ms = new MemoryStream(bytes);
ContentType ct = new ContentType();
//附件文件类型
ct.MediaType = MediaTypeNames.Text.Html;
//附件名称,可以是其它后缀名
ct.Name = "附件名称" + DateTime.Now.ToString() + ".html";
Attachment data = new Attachment(ms, ct);
//防止附件名称中文变乱码
data.NameEncoding = System.Text.Encoding.UTF8;
mmsg.Attachments.Add(data);
//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("account@qq.com");
sc.Send(mmsg);
//释放流资源
ms.Close();
ms.Dispose();