MailSender.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using MailKit.Net.Smtp;
  2. using Microsoft.Extensions.Options;
  3. using MimeKit;
  4. using MTWorkHR.Core.Email;
  5. using MTWorkHR.Core.Entities;
  6. using MTWorkHR.Core.Global;
  7. //using SendGrid;
  8. //using SendGrid.Helpers.Mail;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace MTWorkHR.Infrastructure.EmailService
  15. {
  16. public class MailSender : IMailSender
  17. {
  18. public AppSettingsConfiguration _configuration { get; }
  19. public MailSender(AppSettingsConfiguration configuration)
  20. {
  21. _configuration = configuration;
  22. }
  23. //public async Task<bool> SendEmailGrid(EmailMessage email)
  24. //{
  25. // var client = new SendGridClient(_configuration.MailSettings.ApiKey);
  26. // var to = new EmailAddress(email.To);
  27. // var from = new EmailAddress
  28. // {
  29. // Email = _configuration.MailSettings.FromAddress,
  30. // Name = _configuration.MailSettings.FromName
  31. // };
  32. // email.Body = email.Body + " /n" + email.url;
  33. // var message = MailHelper.CreateSingleEmail(from, to, email.Subject, email.Body, email.Body);
  34. // var response = await client.SendEmailAsync(message);
  35. // return response.IsSuccessStatusCode;
  36. //}
  37. public async Task<bool> SendEmail(EmailMessage email)
  38. {
  39. var mailTo = email.To;
  40. var settings = _configuration.MailSettings;
  41. if (settings.FromAddress == null || settings.FromAddress== "" || settings.Password == null || settings.TemplatePath == null || settings.Host == null || settings.Port == 0 || mailTo == null)
  42. return false;
  43. //string FilePath = _configuration.MailSettings.TemplatePath;
  44. //StreamReader str = new StreamReader(FilePath);
  45. //string MailText = str.ReadToEnd();
  46. //str.Close();
  47. //MailText = MailText.Replace("[NotificationType]", email.Subject).Replace("[Info]", email.Body).Replace("[Link]", email.url );
  48. var emailObj = new MimeMessage();
  49. emailObj.Sender = MailboxAddress.Parse(_configuration.MailSettings.FromAddress);
  50. emailObj.To.Add(MailboxAddress.Parse(mailTo));
  51. emailObj.Subject = email.Subject;
  52. var builder = new BodyBuilder();
  53. email.Body = email.Body +
  54. ( string.IsNullOrEmpty( email.url ) ?"": "Please click on <a href =\"" + email.url + "\">this link</a> to confirm your email address is correct. ");
  55. builder.HtmlBody = email.Body;
  56. emailObj.Body = builder.ToMessageBody();
  57. try
  58. {
  59. using var smtp = new SmtpClient();
  60. smtp.Connect(_configuration.MailSettings.Host, _configuration.MailSettings.Port, MailKit.Security.SecureSocketOptions.StartTls);
  61. smtp.Authenticate(_configuration.MailSettings.FromAddress, _configuration.MailSettings.Password);
  62. await smtp.SendAsync(emailObj);
  63. smtp.Disconnect(true);
  64. }catch (Exception ex) { var ms = ex.Message; }
  65. return true;
  66. }
  67. }
  68. }