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.

120 lines
5.1 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. NewApplication,
  16. NewJob,
  17. ExpiringJob
  18. }
  19. public class SevominEmailer
  20. {
  21. public Dictionary<string, string> Parameters { get; set; }
  22. public EmailType EmailType { get; set; }
  23. private string EmailFolderPath;
  24. private string EmailConfirmationFilePath;
  25. private string NewPasswordFilePath;
  26. private string PasswordResetFilePath;
  27. private string ExpiringJobFilePath;
  28. private string NewJobFilePath;
  29. private string NewApplicationFilePath;
  30. public SevominEmailer()
  31. {
  32. EmailFolderPath = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/app_data"), "emails");
  33. if (!Directory.Exists(EmailFolderPath))
  34. throw new ApplicationException("Emails folder does not exist in the right address.");
  35. EmailConfirmationFilePath = Path.Combine(EmailFolderPath, "email-confirmation.html");
  36. NewPasswordFilePath = Path.Combine(EmailFolderPath, "new-password.html");
  37. PasswordResetFilePath = Path.Combine(EmailFolderPath, "password-reset.html");
  38. ExpiringJobFilePath = Path.Combine(EmailFolderPath, "expiring-job.html");
  39. NewJobFilePath = Path.Combine(EmailFolderPath, "new-job.html");
  40. NewApplicationFilePath = Path.Combine(EmailFolderPath, "new-application.html");
  41. if(!File.Exists(EmailConfirmationFilePath))
  42. throw new ApplicationException("Email confirmation template does not exist in the right address.");
  43. if (!File.Exists(NewPasswordFilePath))
  44. throw new ApplicationException("New password template does not exist in the right address.");
  45. if (!File.Exists(PasswordResetFilePath))
  46. throw new ApplicationException("Password reset template does not exist in the right address.");
  47. if (!File.Exists(ExpiringJobFilePath))
  48. throw new ApplicationException("Expiring job template does not exist in the right address.");
  49. if (!File.Exists(NewJobFilePath))
  50. throw new ApplicationException("New job template does not exist in the right address.");
  51. if (!File.Exists(NewApplicationFilePath))
  52. throw new ApplicationException("New application template does not exist in the right address.");
  53. Parameters = new Dictionary<string, string>();
  54. }
  55. public SevominEmailer(Dictionary<string, string> parameters, EmailType emailType) : this()
  56. {
  57. Parameters = parameters;
  58. EmailType = emailType;
  59. }
  60. public async Task SendAsync(string to, string subject, bool isHtml)
  61. {
  62. SmtpClient client = new SmtpClient();
  63. MailMessage msg = new MailMessage();
  64. string template;
  65. switch (EmailType)
  66. {
  67. case EmailType.EmailConfirmation:
  68. template =
  69. File.ReadAllText(EmailConfirmationFilePath, Encoding.UTF8);
  70. break;
  71. case EmailType.PasswordReset:
  72. template =
  73. File.ReadAllText(PasswordResetFilePath, Encoding.UTF8);
  74. break;
  75. case EmailType.NewPassword:
  76. template =
  77. File.ReadAllText(NewPasswordFilePath, Encoding.UTF8);
  78. break;
  79. case EmailType.ExpiringJob:
  80. template =
  81. File.ReadAllText(ExpiringJobFilePath, Encoding.UTF8);
  82. break;
  83. case EmailType.NewApplication:
  84. template =
  85. File.ReadAllText(NewApplicationFilePath, Encoding.UTF8);
  86. break;
  87. case EmailType.NewJob:
  88. template =
  89. File.ReadAllText(NewJobFilePath, Encoding.UTF8);
  90. break;
  91. default:
  92. template = string.Empty;
  93. break;
  94. }
  95. foreach (var address in to.Split(','))
  96. msg.To.Add(address);
  97. msg.From = new MailAddress("[email protected]", "سومین - مرکز کاریابی کنترل پروژه");
  98. msg.SubjectEncoding = Encoding.UTF8;
  99. msg.BodyEncoding = Encoding.UTF8;
  100. msg.Subject = subject;
  101. msg.IsBodyHtml = isHtml;
  102. Func<string> getBody = () =>
  103. {
  104. foreach (var param in Parameters)
  105. template = template.Replace(string.Format("[{0}]", param.Key), param.Value);
  106. return template;
  107. };
  108. msg.Body = getBody();
  109. await Task.Run(() =>
  110. {
  111. client.Send(msg);
  112. });
  113. }
  114. }
  115. }