HotmailEmailService.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 
  2. using System.Net;
  3. using System.Net.Mail;
  4. namespace OASystem.API.OAMethodLib.HotmailEmail
  5. {
  6. /// <summary>
  7. /// Hotemail 服务
  8. /// </summary>
  9. public class HotmailEmailService
  10. {
  11. private readonly string _hotmailEmail = "your-email@hotmail.com";
  12. private readonly string _appPassword = "your-app-specific-password"; // 不是你的登录密码,是应用专用密码
  13. public async Task SendEmailAsync(string targetEmail, string subject, string body)
  14. {
  15. using (var client = new SmtpClient("smtp.office365.com", 587))
  16. {
  17. client.EnableSsl = true;
  18. client.UseDefaultCredentials = false;
  19. client.Credentials = new NetworkCredential(_hotmailEmail, _appPassword);
  20. var mailMessage = new MailMessage
  21. {
  22. From = new MailAddress(_hotmailEmail, "商邀联络官"),
  23. Subject = subject,
  24. Body = body,
  25. IsBodyHtml = true // 如果内容是 HTML 格式
  26. };
  27. mailMessage.To.Add(targetEmail);
  28. try
  29. {
  30. await client.SendMailAsync(mailMessage);
  31. }
  32. catch (Exception ex)
  33. {
  34. // 记录日志:ex.Message
  35. throw;
  36. }
  37. }
  38. }
  39. }
  40. }