MailUtil.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. string strMail = ConfigHelper.GetSectionValue("MailSetting:mail");
  17. MailboxAddress from = new MailboxAddress("绩效系统",strMail);
  18. message.From.Add(from);
  19. MailboxAddress to = new MailboxAddress(toMailName, toEmail);
  20. message.To.Add(to);
  21. message.Subject = subject;
  22. BodyBuilder bodyBuilder = new BodyBuilder();
  23. bodyBuilder.HtmlBody = body;
  24. message.Body = bodyBuilder.ToMessageBody();
  25. SmtpClient client = new SmtpClient();
  26. var strServer = ConfigHelper.GetSectionValue("MailSetting:Server");
  27. var strPort = ConfigHelper.GetSectionValue("MailSetting:Port");
  28. var strAccount = ConfigHelper.GetSectionValue("MailSetting:Account");
  29. var strPassword = ConfigHelper.GetSectionValue("MailSetting:Password");
  30. client.Connect(strServer , int.Parse(strPort),true); //例如:smtp.exmail.qq.com,465
  31. client.Authenticate(strAccount, strPassword); //发送邮件的账户密码
  32. client.Send(message);
  33. client.Disconnect(true);
  34. client.Dispose();
  35. }
  36. private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
  37. {
  38. if(e.Error != null){
  39. System.Diagnostics.Debug.WriteLine(e.Error.ToString());
  40. }
  41. }
  42. }
  43. }