EmailSenderBase.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System.Text;
  2. using System.Threading.Tasks;
  3. using Abp.Extensions;
  4. using System.Net.Mail;
  5. namespace Abp.Net.Mail
  6. {
  7. /// <summary>
  8. /// This class can be used as base to implement <see cref="IEmailSender"/>.
  9. /// </summary>
  10. public abstract class EmailSenderBase : IEmailSender
  11. {
  12. public IEmailSenderConfiguration Configuration { get; }
  13. /// <summary>
  14. /// Constructor.
  15. /// </summary>
  16. /// <param name="configuration">Configuration</param>
  17. protected EmailSenderBase(IEmailSenderConfiguration configuration)
  18. {
  19. Configuration = configuration;
  20. }
  21. public virtual async Task SendAsync(string to, string subject, string body, bool isBodyHtml = true)
  22. {
  23. await SendAsync(new MailMessage
  24. {
  25. To = { to },
  26. Subject = subject,
  27. Body = body,
  28. IsBodyHtml = isBodyHtml
  29. });
  30. }
  31. public virtual void Send(string to, string subject, string body, bool isBodyHtml = true)
  32. {
  33. Send(new MailMessage
  34. {
  35. To = { to },
  36. Subject = subject,
  37. Body = body,
  38. IsBodyHtml = isBodyHtml
  39. });
  40. }
  41. public virtual async Task SendAsync(string from, string to, string subject, string body, bool isBodyHtml = true)
  42. {
  43. await SendAsync(new MailMessage(from, to, subject, body) { IsBodyHtml = isBodyHtml });
  44. }
  45. public virtual void Send(string from, string to, string subject, string body, bool isBodyHtml = true)
  46. {
  47. Send(new MailMessage(from, to, subject, body) { IsBodyHtml = isBodyHtml });
  48. }
  49. public virtual async Task SendAsync(MailMessage mail, bool normalize = true)
  50. {
  51. if (normalize)
  52. {
  53. NormalizeMail(mail);
  54. }
  55. await SendEmailAsync(mail);
  56. }
  57. public virtual void Send(MailMessage mail, bool normalize = true)
  58. {
  59. if (normalize)
  60. {
  61. NormalizeMail(mail);
  62. }
  63. SendEmail(mail);
  64. }
  65. /// <summary>
  66. /// Should implement this method to send email in derived classes.
  67. /// </summary>
  68. /// <param name="mail">Mail to be sent</param>
  69. protected abstract Task SendEmailAsync(MailMessage mail);
  70. /// <summary>
  71. /// Should implement this method to send email in derived classes.
  72. /// </summary>
  73. /// <param name="mail">Mail to be sent</param>
  74. protected abstract void SendEmail(MailMessage mail);
  75. /// <summary>
  76. /// Normalizes given email.
  77. /// Fills <see cref="MailMessage.From"/> if it's not filled before.
  78. /// Sets encodings to UTF8 if they are not set before.
  79. /// </summary>
  80. /// <param name="mail">Mail to be normalized</param>
  81. protected virtual void NormalizeMail(MailMessage mail)
  82. {
  83. if (mail.From == null || mail.From.Address.IsNullOrEmpty())
  84. {
  85. mail.From = new MailAddress(
  86. Configuration.DefaultFromAddress,
  87. Configuration.DefaultFromDisplayName,
  88. Encoding.UTF8
  89. );
  90. }
  91. if (mail.HeadersEncoding == null)
  92. {
  93. mail.HeadersEncoding = Encoding.UTF8;
  94. }
  95. if (mail.SubjectEncoding == null)
  96. {
  97. mail.SubjectEncoding = Encoding.UTF8;
  98. }
  99. if (mail.BodyEncoding == null)
  100. {
  101. mail.BodyEncoding = Encoding.UTF8;
  102. }
  103. }
  104. }
  105. }