-
-
Save andrewdavey/72d25bd4bcbbf5cba8d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MailSender | |
{ | |
// Call this from Application_Start in Global.asax.cs | |
public static void Init() | |
{ | |
var viewRenderer = new EmailViewRenderer(ViewEngines.Engines) {EmailViewDirectoryName = "Emails"}; | |
var emailParser = new EmailParser(viewRenderer); | |
Email.CreateEmailService = ()=>new EmailService(viewRenderer, new PremailerEmailParser(emailParser), ()=>new SmtpClient()); | |
} | |
private class PremailerEmailParser : IEmailParser | |
{ | |
private readonly IEmailParser wrapped; | |
public PremailerEmailParser(IEmailParser wrapped) | |
{ | |
this.wrapped = wrapped; | |
} | |
public MailMessage Parse(string emailViewOutput, Email email) | |
{ | |
var message = wrapped.Parse(emailViewOutput, email); | |
if (message.IsBodyHtml) | |
{ | |
message.Body = PreMailer.Net.PreMailer.MoveCssInline(message.Body).Html; | |
} | |
for(var i = 0 ; i < message.AlternateViews.Count ; i++) | |
{ | |
var av = message.AlternateViews[i]; | |
if (av.ContentType.MediaType == MediaTypeNames.Text.Html) | |
{ | |
av.ContentStream.Position = 0; | |
var content = new StreamReader(av.ContentStream).ReadToEnd(); | |
content = PreMailer.Net.PreMailer.MoveCssInline(content).Html; | |
message.AlternateViews[i] = AlternateView.CreateAlternateViewFromString(content, av.ContentType); | |
} | |
} | |
return message; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment