title: ASP.NET MVC Email
body: <p>Is their a solution to generate an email template using an ASP.NET MVC View without having to jump through hoops.</p>
  
  <p>Let me elaborate jumping through hoops.</p>
  
  <pre><code>var fakeContext = new HttpContext(HttpContext.Current.Request, fakeResponse);
  var oldContext = HttpContext.Current;
  HttpContext.Current = fakeContext;
  var html = new HtmlHelper(new ViewContext(fakeControllerContext,
    new FakeView(), viewDataDictionary, new TempDataDictionary()),
    new ViewPage());
  html.RenderPartial(viewName, viewData, viewDataDictionary);
  HttpContext.Current = oldContext;
  </code></pre>
  
  <p><em>The above code is using the current HttpContext to fake a new Context and render the page with RenderPartial, we shouldn't have to do this.</em></p>
  
  <p>Another very detailed solution using ControllerContext and .Render: 
  (<a href="http://cowfarm.net/?p=81" rel="nofollow noreferrer">IEmailTemplateService, Headers/Postback WorkAround</a>) but pretty much doing the same thing with a lot more code.</p>
  
  <p>I on the other hand, am looking for something that would just render a View without having to POST/GET and generates me a simple string that I can send off through my Email code. 
  Something that doesn't run into errors such as posting headers twice or faking some piece of data.</p>
  
  <p><strong>EX:</strong></p>
  
  <pre><code>//code which does not fire Render, RenderPartial... etc
  var email = emailFramework.Create(viewData, view); 
  </code></pre>
  
  <hr>
  
  <p>See my solution bellow or follow this link:</p>
  
  <p><strong>My Solution using spark: (12/30/2009) <a href="http://cowfarm.net/?p=188" rel="nofollow noreferrer">ASP.NET MVC Email Template Solution</a></strong> </p>