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.

135 lines
5.5 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Configuration;
  8. using System.Net.Mail;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Web.Configuration;
  12. namespace Sevomin.Models.Helpers
  13. {
  14. public enum EmailType
  15. {
  16. EmailConfirmation,
  17. PasswordReset,
  18. NewPassword,
  19. NewApplication,
  20. NewJob,
  21. ExpiringJob
  22. }
  23. public class SevominEmailer
  24. {
  25. public Dictionary<string, string> Parameters { get; set; }
  26. public EmailType EmailType { get; set; }
  27. private string EmailFolderPath;
  28. private string EmailConfirmationFilePath;
  29. private string PasswordResetFilePath;
  30. private string ExpiringJobFilePath;
  31. private string NewJobFilePath;
  32. private string NewApplicationFilePath;
  33. public SevominEmailer()
  34. {
  35. EmailFolderPath = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/app_data"), "emails");
  36. if (!Directory.Exists(EmailFolderPath))
  37. throw new ApplicationException("Emails folder does not exist in the right address.");
  38. EmailConfirmationFilePath = Path.Combine(EmailFolderPath, "email-confirmation.html");
  39. PasswordResetFilePath = Path.Combine(EmailFolderPath, "password-reset.html");
  40. ExpiringJobFilePath = Path.Combine(EmailFolderPath, "expiring-job.html");
  41. NewJobFilePath = Path.Combine(EmailFolderPath, "new-job.html");
  42. NewApplicationFilePath = Path.Combine(EmailFolderPath, "new-application.html");
  43. if(!File.Exists(EmailConfirmationFilePath))
  44. throw new ApplicationException("Email confirmation 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, bool isHtml, bool optedOut)
  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. msg.Subject = "تایید عضویت";
  71. break;
  72. case EmailType.PasswordReset:
  73. template =
  74. File.ReadAllText(PasswordResetFilePath, Encoding.UTF8);
  75. msg.Subject = "بازیابی رمز عبور";
  76. break;
  77. case EmailType.ExpiringJob:
  78. template =
  79. File.ReadAllText(ExpiringJobFilePath, Encoding.UTF8);
  80. msg.Subject = "اتمام اعتبار آگهی";
  81. break;
  82. case EmailType.NewApplication:
  83. template =
  84. File.ReadAllText(NewApplicationFilePath, Encoding.UTF8);
  85. msg.Subject = "آمادگی جدید";
  86. break;
  87. case EmailType.NewJob:
  88. template =
  89. File.ReadAllText(NewJobFilePath, Encoding.UTF8);
  90. msg.Subject = "آگهی استخدام جدید";
  91. break;
  92. default:
  93. template = string.Empty;
  94. msg.Subject = string.Empty;
  95. break;
  96. }
  97. if (!optedOut)
  98. {
  99. foreach (var address in to.Split(','))
  100. msg.To.Add(address);
  101. //msg.Bcc.Add("[email protected]");
  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. msg.From = new MailAddress("[email protected]");
  110. msg.SubjectEncoding = Encoding.UTF8;
  111. msg.BodyEncoding = Encoding.UTF8;
  112. msg.IsBodyHtml = isHtml;
  113. client.Port = 587;
  114. client.Credentials = new System.Net.NetworkCredential("[email protected]", "4XdWMRVFBO-pow4I1md1Bw");
  115. client.EnableSsl = true;
  116. await Task.Run(() =>
  117. {
  118. client.Send(msg);
  119. });
  120. }
  121. }
  122. }
  123. }