using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Net.Mail; using System.Text; using System.Threading.Tasks; namespace wispro.sp.utility { public class MailUtil { public static void SendEmail(string subject, string body, string toEmail) { // 命令行参数必须是SMTP主机 SmtpClient client = new SmtpClient(ConfigHelper.GetSectionValue("MailSetting:Server"), int.Parse(ConfigHelper.GetSectionValue("MailSetting:Port"))); string User = ConfigHelper.GetSectionValue("MailSetting:Account"); string PassWord = ConfigHelper.GetSectionValue("MailSetting:Password"); // 服务平台获取 client.UseDefaultCredentials = false; client.EnableSsl = true; client.Credentials = new System.Net.NetworkCredential(User, PassWord); // 发送人 MailAddress from = new MailAddress(ConfigHelper.GetSectionValue("MailSetting:mail"), "绩效系统", Encoding.UTF8); // 接收人 MailAddress to = new MailAddress(toEmail); // 指定邮件内容 MailMessage message = new MailMessage(from, to); message.Body = body; message.BodyEncoding = Encoding.UTF8; message.IsBodyHtml = true; // 主题 message.Subject = subject; message.SubjectEncoding = Encoding.UTF8; // 设置发送操作结束时回调的方法. client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback); string userState = subject; client.SendAsync(message,null); //Console.WriteLine("发送消息..."); } private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e) { if(e.Error != null){ System.Diagnostics.Debug.WriteLine(e.Error.ToString()); } } } }