Sending emails sans layout

by benl

Monorail controllers provide you with nice templated email sending infrastructure. However, when rendering your email, Monorail insists on mixing it up with the layout specified on your controller. Not good if you don’t want this included. You can solve this by moving your emailing out into another controller and specifying the layout there, or you can solve it in a slightly more long-winded fashion by using RenderInPlaceSharedView and passing off the result of this to your own custom email sending service. Why not implement asynchronous sending while you’re at it? 😉

Below is an example I’ve used in one of my projects for rendering a shared view into a string:

string email = "";
string productUrl = productService.GetProductUrl(product);
PropertyBag["productUrl"] = productUrl;
using(StringWriter sw = new StringWriter())
InPlaceRenderSharedView(sw, "mails/stocknotification");
email = sw.ToString();

return email;