You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net.Mail;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace Sevomin.Models.Helpers
  9. {
  10. public enum EmailType
  11. {
  12. EmailConfirmation,
  13. PasswordReset,
  14. NewPassword
  15. }
  16. public class SevominEmailer
  17. {
  18. public Dictionary<string, string> Parameters { get; set; }
  19. public EmailType EmailType { get; set; }
  20. private string EmailFolderPath;
  21. private string EmailConfirmationFilePath;
  22. private string NewPasswordFilePath;
  23. private string PasswordResetFilePath;
  24. public SevominEmailer()
  25. {
  26. EmailFolderPath = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/app_data"), "emails");
  27. if (!Directory.Exists(EmailFolderPath))
  28. throw new ApplicationException("Emails folder does not exist in the right address.");
  29. EmailConfirmationFilePath = Path.Combine(EmailFolderPath, "email-confirmation.html");
  30. NewPasswordFilePath = Path.Combine(EmailFolderPath, "new-password.html");
  31. PasswordResetFilePath = Path.Combine(EmailFolderPath, "password-reset.html");
  32. if(!File.Exists(EmailConfirmationFilePath))
  33. throw new ApplicationException("Email confirmation template does not exist in the right address.");
  34. if (!File.Exists(NewPasswordFilePath))
  35. throw new ApplicationException("New password template does not exist in the right address.");
  36. if (!File.Exists(PasswordResetFilePath))
  37. throw new ApplicationException("Password reset template does not exist in the right address.");
  38. Parameters = new Dictionary<string, string>();
  39. }
  40. public SevominEmailer(Dictionary<string, string> parameters, EmailType emailType) : this()
  41. {
  42. Parameters = parameters;
  43. EmailType = emailType;
  44. }
  45. public async Task SendAsync(string to, string subject, bool isHtml)
  46. {
  47. SmtpClient client = new SmtpClient();
  48. MailMessage msg = new MailMessage();
  49. string template;
  50. switch (EmailType)
  51. {
  52. case EmailType.EmailConfirmation:
  53. template =
  54. File.ReadAllText(EmailConfirmationFilePath, Encoding.UTF8);
  55. break;
  56. case EmailType.PasswordReset:
  57. template =
  58. File.ReadAllText(PasswordResetFilePath, Encoding.UTF8);
  59. break;
  60. case EmailType.NewPassword:
  61. template =
  62. File.ReadAllText(NewPasswordFilePath, Encoding.UTF8);
  63. break;
  64. default:
  65. template = string.Empty;
  66. break;
  67. }
  68. foreach (var address in to.Split(','))
  69. msg.To.Add(address);
  70. msg.From = new MailAddress("[email protected]", "سومین - مرکز کاریابی کنترل پروژه");
  71. msg.SubjectEncoding = Encoding.UTF8;
  72. msg.BodyEncoding = Encoding.UTF8;
  73. msg.Subject = subject;
  74. msg.IsBodyHtml = isHtml;
  75. Func<string> getBody = () =>
  76. {
  77. foreach (var param in Parameters)
  78. template = template.Replace(string.Format("[{0}]", param.Key), param.Value);
  79. return template;
  80. };
  81. msg.Body = getBody();
  82. await Task.Run(() =>
  83. {
  84. client.Send(msg);
  85. });
  86. }
  87. }
  88. }