I'm trying to send formatted emails in my MVC project. I had normal emails being sent perfectly with this method in my BaseService class, which all other services inherit from:
public void SendAsyncEmail(MailMessage message)
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("cred", "password"),
EnableSsl = true
};
client.SendCompleted += (s, e) =>
{
client.Dispose();
message.Dispose();
};
ThreadPool.QueueUserWorkItem(o => client.SendAsync(message, Tuple.Create(client, message)));
}
Now, instead of a text-only body, I need to transform every MailMessage.Body into an HTML page, ideally by using Razor templating. I found MVCMailer:
https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide
I installed it, scaffolded some templates, and added the following in my BaseService:
public virtual MvcMailMessage Welcome()
{
ViewBag.Name = "Test";
return Populate(x =>
{
x.viewName = "Welcome";
x.To.Add("sohan39@example.com");
});
}
But my service doesn't have an HTTPContext, so ViewBag isn't resolving (nor Populate).
What's the simplest way to get this working?