private static void SendEmail(string mailSubject, List<string> mailAddress, List<string> ccs, string mailContent, List<string> mailAttachment)
{
try
{
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
smtpClient.Host = smtpServer; //指定SMTP服务器
smtpClient.UseDefaultCredentials = true;
//string userPassword = "&R$CEg=6";//登陆密码
//smtpClient.UseDefaultCredentials = false;
//smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, userPassword);//用户名和密码
// 发送邮件设置
MailMessage mailMessage = new MailMessage(); // 发送人和收件人
MailAddress mf = new MailAddress(mailFrom, dispalyName);
mailMessage.From = mf;
foreach (string current in mailAddress)
{
if (!string.IsNullOrEmpty(current) && current.IndexOf('@') > 0)
{
mailMessage.To.Add(current);
}
}
if (ccs != null)
{
foreach (string current2 in ccs)
{
if (!string.IsNullOrEmpty(current2) && current2.IndexOf('@') > 0)
{
mailMessage.CC.Add(current2);
}
}
}
mailMessage.Body = mailContent;
mailMessage.BodyEncoding = Encoding.UTF8;
mailMessage.IsBodyHtml = true;
mailMessage.Subject = mailSubject;
mailMessage.SubjectEncoding = Encoding.UTF8;
mailMessage.Priority = MailPriority.High;
if (mailAttachment != null && mailAttachment.Count > 0)
{
foreach (string current3 in mailAttachment)
{
if (File.Exists(current3))
{
Attachment item = new Attachment(current3);
mailMessage.Attachments.Add(item);
}
}
}
smtpClient.Send(mailMessage);
log.Info("send e-mail success!");
}
catch (Exception e)
{
log.Error(string.Format("
内部错误:{0}
堆栈:{1}
Message:{2}
来源:{3}
",
e.InnerException==null?e.Message:e.InnerException.ToString(), e.StackTrace, e.Message, e.Source));
//throw e;
}
}