Views are essential part of ASP.NET MVC and we can take advantage of Views in situations like where we need email template to be sent on user registration. So here are the steps I have done in order to use a view as email message body.
- Styles must be inline in your View.
- Render your View by passing Model.
- Get HTML (string) result to set it as email body.
private string ViewToString(string viewName, object model)
{
ViewData.Model = model;
using (var sw = new System.IO.StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}
0 comments:
Post a Comment