private static MailMessage CreateMailMessage(Email email) { var message = new MailMessage { From = new MailAddress(email.From), Subject = email.Subject }; if (!String.IsNullOrWhiteSpace(email.Sender)) { message.Sender = new MailAddress(email.Sender); } email.To.Each(to => message.To.Add(to)); email.ReplyTo.Each(to => message.ReplyToList.Add(to)); email.CC.Each(cc => message.CC.Add(cc)); email.Bcc.Each(bcc => message.Bcc.Add(bcc)); email.Headers.Each(pair => message.Headers[pair.Key] = pair.Value); if (!String.IsNullOrWhiteSpace(email.HtmlBody) && !String.IsNullOrWhiteSpace(email.TextBody)) { message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(email.HtmlBody, new ContentType(ContentTypes.Html))); message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(email.TextBody, new ContentType(ContentTypes.Text))); } else if (!String.IsNullOrWhiteSpace(email.TextBody)) { message.Body = email.TextBody; message.IsBodyHtml = false; } else if (!String.IsNullOrWhiteSpace(email.HtmlBody)) { message.Body = email.HtmlBody; message.IsBodyHtml = true; } return message; }
public Email RenderTemplate(string templateName, object model = null) { if (String.IsNullOrWhiteSpace(templateName)) { throw new System.ArgumentException(String.Format(System.Globalization.CultureInfo.CurrentUICulture, "\"{0}\" cannot be blank.", "templateName")); } var templates = CreateTemplateInstances(templateName); foreach (var pair in templates) { pair.Value.SetModel(CreateModel(model)); pair.Value.Execute(); } var mail = new Email(); templates.SelectMany(x => x.Value.To) .Distinct(StringComparer.OrdinalIgnoreCase) .Each(email => mail.To.Add(email)); templates.SelectMany(x => x.Value.ReplyTo) .Distinct(StringComparer.OrdinalIgnoreCase) .Each(email => mail.ReplyTo.Add(email)); templates.SelectMany(x => x.Value.Bcc) .Distinct(StringComparer.OrdinalIgnoreCase) .Each(email => mail.Bcc.Add(email)); templates.SelectMany(x => x.Value.CC) .Distinct(StringComparer.OrdinalIgnoreCase) .Each(email => mail.CC.Add(email)); IEmailTemplate template = null; // text template (.text.cshtml file) if (templates.TryGetValue(ContentTypes.Text, out template)) { SetProperties(template, mail, body => { mail.TextBody = body; }); } // html template (.html.cshtml file) if (templates.TryGetValue(ContentTypes.Html, out template)) { SetProperties(template, mail, body => { mail.HtmlBody = body; }); } // shared template (.cshtml file) if (templates.TryGetValue(String.Empty, out template)) { SetProperties(template, mail, null); } return mail; }
public void Send(Email email) { if (email == null) { throw new System.ArgumentNullException("email"); } using (var message = CreateMailMessage(email)) { using (var client = new SmtpClient()) { client.Send(message); } } }
private static void SetProperties(IEmailTemplate template, Email mail, Action<string> updateBody) { if (template != null) { if (!String.IsNullOrWhiteSpace(template.From)) { mail.From = template.From; } if (!String.IsNullOrWhiteSpace(template.Sender)) { mail.Sender = template.Sender; } if (!String.IsNullOrWhiteSpace(template.Subject)) { mail.Subject = template.Subject; } template.Headers.Each(pair => mail.Headers[pair.Key] = pair.Value); if (updateBody != null) { updateBody(template.Body); } } }