MailUtil.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Net.Mail;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace wispro.sp.utility
  9. {
  10. public class MailUtil
  11. {
  12. public static void SendEmail(string subject, string body, string toEmail)
  13. {
  14. // 命令行参数必须是SMTP主机
  15. SmtpClient client = new SmtpClient(ConfigHelper.GetSectionValue("MailSetting:Server"), int.Parse(ConfigHelper.GetSectionValue("MailSetting:Port")));
  16. string User = ConfigHelper.GetSectionValue("MailSetting:Account");
  17. string PassWord = ConfigHelper.GetSectionValue("MailSetting:Password"); // 服务平台获取
  18. client.UseDefaultCredentials = false;
  19. client.EnableSsl = true;
  20. client.Credentials = new System.Net.NetworkCredential(User, PassWord);
  21. // 发送人
  22. MailAddress from = new MailAddress(ConfigHelper.GetSectionValue("MailSetting:mail"), "绩效系统", Encoding.UTF8);
  23. // 接收人
  24. MailAddress to = new MailAddress(toEmail);
  25. // 指定邮件内容
  26. MailMessage message = new MailMessage(from, to);
  27. message.Body = body;
  28. message.BodyEncoding = Encoding.UTF8;
  29. message.IsBodyHtml = true;
  30. // 主题
  31. message.Subject = subject;
  32. message.SubjectEncoding = Encoding.UTF8;
  33. // 设置发送操作结束时回调的方法.
  34. client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
  35. string userState = subject;
  36. client.SendAsync(message,null);
  37. //Console.WriteLine("发送消息...");
  38. }
  39. private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
  40. {
  41. if(e.Error != null){
  42. System.Diagnostics.Debug.WriteLine(e.Error.ToString());
  43. }
  44. }
  45. }
  46. }