title: Sending an Email asynchronously via ActionMailer
body: <p>I'm trying to get an email to send asynchronously via ActionMailer for MVC3/MVC4 for C# ASP.NET.</p>
  
  <p>However I have come across 2 problems:</p>
  
  <ol>
  <li><p>If I attempt to use a Task.Run or Task.Start method using a private function, then the HttpContext becomes null, and the email will not send. This is also the case if I use the inbuilt async delivery method.</p></li>
  <li><p>If I attempt to put async/await on the email sending function, that does not work because the return method is void, and I've been told that you cannot await on void functions.</p></li>
  </ol>
  
  <p>Here is the code I've used for both scenarios (in both cases, assume e is a Model object):</p>
  
  <p>Scenario 1:</p>
  
  <pre><code>Action mailAction = new Action(() =&gt; { OnSentEmail(e); });
  Task mailTask = new Task(mailAction);
  mailTask.Start();
  
  private void OnSentEmail(Model e)
  {
      new MailController().SendMail(e).Deliver();
      //HttpContext is null here, so cannot send mail
  }
  
  OR
  
  new MailController().SendMail(e).DeliverAsync();
  </code></pre>
  
  <p>Scenario 2:</p>
  
  <pre><code>await Task.Run(async() =&gt; await new MailController().SendMail(e).Deliver());
  </code></pre>
  
  <p>I'm not very good with async methods yet, but this seemed to be the best I could come up with. I cannot use regular SmtpClient because the emails that need to be sent out are in rich HTML format, so this is why I chose ActionMailer so I can send Emails and style them as though they were views.</p>
  
  <p>What I need is to be able to send the Email asynchronously, either by forcing in a HttpContext (I don't know what information the MailerBase uses from the HttpContext to send the emails), or to be able to async/await it.</p>