MailUtil.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using MailKit.Net.Smtp;
  8. using MimeKit;
  9. namespace wispro.sp.utility
  10. {
  11. public class MailUtil
  12. {
  13. public static void SendEmail(string subject, string body,string toMailName, string toEmail)
  14. {
  15. MimeMessage message = new MimeMessage();
  16. MailboxAddress from = new MailboxAddress("绩效系统",ConfigHelper.GetSectionValue("MailSetting:mail"));
  17. message.From.Add(from);
  18. MailboxAddress to = new MailboxAddress(toMailName, toEmail);
  19. message.To.Add(to);
  20. message.Subject = subject;
  21. BodyBuilder bodyBuilder = new BodyBuilder();
  22. bodyBuilder.HtmlBody = body;
  23. message.Body = bodyBuilder.ToMessageBody();
  24. SmtpClient client = new SmtpClient();
  25. client.Connect(ConfigHelper.GetSectionValue("MailSetting:Server"), int.Parse(ConfigHelper.GetSectionValue("MailSetting:Port")),true); //例如:smtp.exmail.qq.com,465
  26. client.Authenticate(ConfigHelper.GetSectionValue("MailSetting:Account"), ConfigHelper.GetSectionValue("MailSetting:Password")); //发送邮件的账户密码
  27. client.Send(message);
  28. client.Disconnect(true);
  29. client.Dispose();
  30. }
  31. private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
  32. {
  33. if(e.Error != null){
  34. System.Diagnostics.Debug.WriteLine(e.Error.ToString());
  35. }
  36. }
  37. }
  38. }