using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using MailKit.Net.Smtp; using MimeKit; namespace wispro.sp.utility { public class MailUtil { public static void SendEmail(string subject, string body,string toMailName, string toEmail) { MimeMessage message = new MimeMessage(); MailboxAddress from = new MailboxAddress("绩效系统",ConfigHelper.GetSectionValue("MailSetting:mail")); message.From.Add(from); MailboxAddress to = new MailboxAddress(toMailName, toEmail); message.To.Add(to); message.Subject = subject; BodyBuilder bodyBuilder = new BodyBuilder(); bodyBuilder.HtmlBody = body; message.Body = bodyBuilder.ToMessageBody(); SmtpClient client = new SmtpClient(); client.Connect(ConfigHelper.GetSectionValue("MailSetting:Server"), int.Parse(ConfigHelper.GetSectionValue("MailSetting:Port")),true); //例如:smtp.exmail.qq.com,465 client.Authenticate(ConfigHelper.GetSectionValue("MailSetting:Account"), ConfigHelper.GetSectionValue("MailSetting:Password")); //发送邮件的账户密码 client.Send(message); client.Disconnect(true); client.Dispose(); } private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e) { if(e.Error != null){ System.Diagnostics.Debug.WriteLine(e.Error.ToString()); } } } }