My application is split into three tiers:
- View (MVC)
- Business logic (Services)
- Data (LINQ / SQL)
Throughout my Services layer, I have calls to an EmailSender service that currently sends plaintext emails via SmtpClient and MailMessage. Pretty standard stuff as far as I know.
I'm now trying to format the body of these emails by using a Razor template that accepts a MailMessage as a model, and generates some HTML that I can stick into the body of the message.
Right now this is what I have:
public void SendAsyncEmail(MailMessage message)
{
message.IsBodyHtml = true;
message.body = {generate html string from body here using razor?}
// Async code here
}
Say I define a razor view like this:
@model System.Net.Mail.MailMessage
<div>
<h1>@Model.Subject</h1>
<p>@Model.Body</p>
</div>
How can I pass the MailMessage into this view from my service method and generate a string from it for the body of the email?