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

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Configuration;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Web.Configuration;
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, bool isHtml, bool optedOut)
{
SmtpClient client = new SmtpClient();
MailMessage msg = new MailMessage();
string template;
switch (EmailType)
{
case EmailType.EmailConfirmation:
template =
File.ReadAllText(EmailConfirmationFilePath, Encoding.UTF8);
msg.Subject = "تایید عضویت";
break;
case EmailType.PasswordReset:
template =
File.ReadAllText(PasswordResetFilePath, Encoding.UTF8);
msg.Subject = "بازیابی رمز عبور";
break;
case EmailType.ExpiringJob:
template =
File.ReadAllText(ExpiringJobFilePath, Encoding.UTF8);
msg.Subject = "اتمام اعتبار آگهی";
break;
case EmailType.NewApplication:
template =
File.ReadAllText(NewApplicationFilePath, Encoding.UTF8);
msg.Subject = "آمادگی جدید";
break;
case EmailType.NewJob:
template =
File.ReadAllText(NewJobFilePath, Encoding.UTF8);
msg.Subject = "آگهی استخدام جدید";
break;
default:
template = string.Empty;
msg.Subject = string.Empty;
break;
}
if (!optedOut)
{
foreach (var address in to.Split(','))
msg.To.Add(address);
//msg.Bcc.Add("[email protected]");
Func<string> getBody = () =>
{
foreach (var param in Parameters)
template = template.Replace(string.Format("[{0}]", param.Key), param.Value);
return template;
};
msg.Body = getBody();
msg.From = new MailAddress("[email protected]");
msg.SubjectEncoding = Encoding.UTF8;
msg.BodyEncoding = Encoding.UTF8;
msg.IsBodyHtml = isHtml;
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("[email protected]", "4XdWMRVFBO-pow4I1md1Bw");
client.EnableSsl = true;
await Task.Run(() =>
{
client.Send(msg);
});
}
}
}
}