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.

113 lines
4.7 KiB

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