![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/// <summary>
///
/// </summary>
/// <param name="mailTo">收件人列表</param>
/// <param name="mailFrom">发件人</param>
/// <param name="mailCC">抄送人列表</param>
/// <param name="mailBcc">密抄人列表</param>
/// <param name="sAttachments">附件列表</param>
/// <param name="mailSubject">标题</param>
/// <param name="mailBody">内容</param>
/// <param name="ishtml">是否支持HTML</param>
public static void Send(string[] mailTo,string mailFrom, string[] mailCC,string[] mailBcc, string[] sAttachments, string mailSubject, string mailBody, bool ishtml) {
if( mailTo == null ) throw new NotImplementedException("收件人不可为空");
if( mailFrom == null ) throw new NotImplementedException( "发件人不可为空" );
if( string.IsNullOrEmpty( mailSubject ) ) throw new NotImplementedException( "标题不可为空" );
MailMessage message = new MailMessage();
//收件人
foreach( var item in mailTo ) {
message.To.Add( new MailAddress( item ) );
}
//抄送列表
foreach( var item in mailCC ) {
message.CC.Add( new MailAddress( item ) );
}
//邮件附件
foreach( var item in sAttachments ) {
message.Attachments.Add( new Attachment( item ) );
}
//密抄人地址
if( mailBcc != null ) {
foreach( var item in mailBcc ) {
message.Bcc.Add( new MailAddress( item ) );
}
}
//发送人地址
message.From = new MailAddress( mailFrom ?? "hebkkun@163.com" );
//邮件标题
Encoding enc = Encoding.GetEncoding("GB2312");
string subjectFormat = "=?{0}?B?{1}?=";
message.Subject = String.Format(subjectFormat, enc.HeaderName,
Convert.ToBase64String(enc.GetBytes(mailSubject), Base64FormattingOptions.None));
message.IsBodyHtml = ishtml;
//邮件正文
message.Body = mailBody;
message.BodyEncoding = Encoding.GetEncoding("GB2312");
SmtpClient client = new SmtpClient(System.Configuration.ConfigurationManager.AppSettings.Get("mailServer"));
//认证
string sMailUser = System.Configuration.ConfigurationManager.AppSettings.Get("smtpUser");
string sMailPass = System.Configuration.ConfigurationManager.AppSettings.Get("smtpPass");
client.Credentials = new NetworkCredential(sMailUser, sMailPass);
try {
client.Send( message );
} catch( SmtpException ex ) {
throw ex;
} finally {
message.Attachments.Dispose();
}
}