引用命名空间:
using System.Net.Mail;
///
/// 发送HTML邮件,有抄送和密送
/// 需要在Web.config文件中的system.net下的mailSettings节点进行配置
///
/// 收件人地址
/// 抄送地址
/// 密送地址
/// 邮件主题
/// 邮件正文
public static void SendMail(string mailto, string mailcc, string mailbcc, string mailsubject, string mailbody)
{
MailMessage mMessage = new MailMessage();
MailAddress mTo = new MailAddress(mailto);
if (mailcc != "") { MailAddress mCC = new MailAddress(mailcc); mMessage.CC.Add(mCC); }
if (mailbcc != "") { MailAddress mBCC = new MailAddress(mailbcc); mMessage.Bcc.Add(mBCC); }
mMessage.To.Add(mTo);
mMessage.Subject = mailsubject;
//正文和主题的字符编码都设置为UTF8
mMessage.SubjectEncoding = System.Text.Encoding.UTF8;
mMessage.BodyEncoding = System.Text.Encoding.UTF8;
mMessage.Body = mailbody;
//正文支持html
mMessage.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Send(mMessage);
} 调用:
SendMail("11111111@qq.com", "", "", "testdata", "hello
");