using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net.Mail;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Sevomin.Models.Helpers
|
|
{
|
|
public enum EmailType
|
|
{
|
|
EmailConfirmation,
|
|
PasswordReset,
|
|
NewPassword,
|
|
NewApplication,
|
|
NewJob,
|
|
ExpiringJob
|
|
}
|
|
|
|
public class SevominEmailer
|
|
{
|
|
public Dictionary<string, string> Parameters { get; set; }
|
|
public EmailType EmailType { get; set; }
|
|
private string EmailFolderPath;
|
|
private string EmailConfirmationFilePath;
|
|
private string PasswordResetFilePath;
|
|
private string ExpiringJobFilePath;
|
|
private string NewJobFilePath;
|
|
private string NewApplicationFilePath;
|
|
public SevominEmailer()
|
|
{
|
|
EmailFolderPath = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/app_data"), "emails");
|
|
if (!Directory.Exists(EmailFolderPath))
|
|
throw new ApplicationException("Emails folder does not exist in the right address.");
|
|
EmailConfirmationFilePath = Path.Combine(EmailFolderPath, "email-confirmation.html");
|
|
PasswordResetFilePath = Path.Combine(EmailFolderPath, "password-reset.html");
|
|
ExpiringJobFilePath = Path.Combine(EmailFolderPath, "expiring-job.html");
|
|
NewJobFilePath = Path.Combine(EmailFolderPath, "new-job.html");
|
|
NewApplicationFilePath = Path.Combine(EmailFolderPath, "new-application.html");
|
|
if(!File.Exists(EmailConfirmationFilePath))
|
|
throw new ApplicationException("Email confirmation template does not exist in the right address.");
|
|
if (!File.Exists(PasswordResetFilePath))
|
|
throw new ApplicationException("Password reset template does not exist in the right address.");
|
|
if (!File.Exists(ExpiringJobFilePath))
|
|
throw new ApplicationException("Expiring job template does not exist in the right address.");
|
|
if (!File.Exists(NewJobFilePath))
|
|
throw new ApplicationException("New job template does not exist in the right address.");
|
|
if (!File.Exists(NewApplicationFilePath))
|
|
throw new ApplicationException("New application template does not exist in the right address.");
|
|
Parameters = new Dictionary<string, string>();
|
|
}
|
|
public SevominEmailer(Dictionary<string, string> parameters, EmailType emailType) : this()
|
|
{
|
|
Parameters = parameters;
|
|
EmailType = emailType;
|
|
}
|
|
|
|
public async Task SendAsync(string to, string subject, bool isHtml)
|
|
{
|
|
SmtpClient client = new SmtpClient();
|
|
MailMessage msg = new MailMessage();
|
|
string template;
|
|
switch (EmailType)
|
|
{
|
|
case EmailType.EmailConfirmation:
|
|
template =
|
|
File.ReadAllText(EmailConfirmationFilePath, Encoding.UTF8);
|
|
break;
|
|
case EmailType.PasswordReset:
|
|
template =
|
|
File.ReadAllText(PasswordResetFilePath, Encoding.UTF8);
|
|
break;
|
|
case EmailType.ExpiringJob:
|
|
template =
|
|
File.ReadAllText(ExpiringJobFilePath, Encoding.UTF8);
|
|
break;
|
|
case EmailType.NewApplication:
|
|
template =
|
|
File.ReadAllText(NewApplicationFilePath, Encoding.UTF8);
|
|
break;
|
|
case EmailType.NewJob:
|
|
template =
|
|
File.ReadAllText(NewJobFilePath, Encoding.UTF8);
|
|
break;
|
|
default:
|
|
template = string.Empty;
|
|
break;
|
|
}
|
|
|
|
foreach (var address in to.Split(','))
|
|
msg.To.Add(address);
|
|
msg.From = new MailAddress("[email protected]", "سومین - مرکز کاریابی کنترل پروژه");
|
|
msg.SubjectEncoding = Encoding.UTF8;
|
|
msg.BodyEncoding = Encoding.UTF8;
|
|
msg.Subject = subject;
|
|
msg.IsBodyHtml = isHtml;
|
|
Func<string> getBody = () =>
|
|
{
|
|
foreach (var param in Parameters)
|
|
template = template.Replace(string.Format("[{0}]", param.Key), param.Value);
|
|
|
|
return template;
|
|
};
|
|
msg.Body = getBody();
|
|
await Task.Run(() =>
|
|
{
|
|
client.Send(msg);
|
|
});
|
|
}
|
|
}
|
|
}
|