Skip to content

Instantly share code, notes, and snippets.

@code-atom
Last active April 7, 2017 04:15
Show Gist options
  • Save code-atom/731e81bb7e216688d318f9a66c81c391 to your computer and use it in GitHub Desktop.
Save code-atom/731e81bb7e216688d318f9a66c81c391 to your computer and use it in GitHub Desktop.
Email Provider for sending email
public class EmailProvider : IEmailProvider
{
private readonly SmtpClient smtpClient = null;
private readonly IEmailTemplateRepository _emailRepository;
private bool disposed = false;
private EmailProvider()
{
smtpClient = new SmtpClient();
}
public EmailProvider(IEmailTemplateRepository emailRepository) : this()
{
_emailRepository = emailRepository;
}
public EmailProvider(string smtpAddress, int portNumber, string userName, string password)
{
smtpClient = new SmtpClient(smtpAddress, portNumber);
smtpClient.Credentials = new NetworkCredential(userName, password);
}
public void Send(MailMessage message)
{
if (disposed) throw new ObjectDisposedException("Email Provider not able to send email after dispose");
if (message == null) throw new ArgumentNullException("Email message argument can't be null");
if (message.To == null || !message.To.Any()) throw new Exception("Recipients are not specified");
var recipientList = string.Join(",", message.To.Select(x => x.Address).ToArray());
try
{
smtpClient.Send(message);
}
catch (Exception ex)
{
throw ex;
}
}
public void Send(string to, string subject, string body = "", bool isHtml = true, string from = "")
{
if (String.IsNullOrEmpty(to) && String.IsNullOrEmpty(subject))
throw new Exception("Recipient or subject can't be null or empty");
var mailMessage = new MailMessage();
var toEmail = new MailAddress(to);
mailMessage.To.Add(toEmail);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = isHtml;
this.Send(mailMessage);
}
public void Send(string to, TemplateTypes type, object bindableContext, string from = "")
{
if (type == 0)
throw new ArgumentNullException("Specify the valid template type");
var content = _emailRepository.GetTemplateByType(type);
if (content == null)
throw new BusinessException(String.Format("No {0} Template exist in system", type.ToString()));
var subjectBindableFunc = Handlebars.Compile(content.EmailSubject);
var bodyBindableFunc = Handlebars.Compile(content.TemplateContent);
Send(to: to, subject:subjectBindableFunc(bindableContext), body:bodyBindableFunc(bindableContext), from: from);
}
private void Dispose(bool disposing)
{
if(disposing)
{
if (!disposed)
{
if (smtpClient != null)
smtpClient.Dispose();
disposed = true;
}
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~EmailProvider()
{
Dispose(false);
}
}
public interface IEmailProvider : IDisposable
{
void Send(MailMessage message);
void Send(string to, string subject, string body = "", bool isHtml = true, string from = "");
void Send(string to, string subject, string templateName, object bindableContext, string from = "");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment