title: Proper way to asynchronously send an email in ASP.NET... (am i doing it right?)
body: <p>When a user registers on my website, I don't see why I need to make him "wait" for the smtp to go through so that he gets an activation email.</p>
  
  <p>I decided I want to launch this code asynchronously, and it's been an adventure.</p>
  
  <p>Lets imagine I have a method, such as:</p>
  
  <pre><code>private void SendTheMail() { // Stuff }
  </code></pre>
  
  <p>My first though.. was threading.  I did this:</p>
  
  <pre><code>Emailer mailer = new Emailer();
  Thread emailThread = new Thread(() =&gt; mailer.SendTheMail());
  emailThread.Start();
  </code></pre>
  
  <p>This works... until I decided to test it for error-handling capability. I purposely broke the SMTP server address in my web.config and tried it.  The scary result was that IIS basically BARFED with an unhandled exception error on w3wp.exe (it was a windows error! how extreme...)  ELMAH (my error logger) did NOT catch it AND IIS was restarted so anyone on the website had their session erased.  Completely unacceptable result!</p>
  
  <p>My next thought, was to do some research on Asynchronous delegates.  This seems to work better because exceptions are being handled within the asynch delegate (unlike the thread example above). However, i'm concerned if i'm doing it wrong or maybe I'm causing memory leaks.</p>
  
  <p>Here's what i'm doing:</p>
  
  <pre><code>Emailer mailer = new Emailer();
  AsyncMethodCaller caller = new AsyncMethodCaller(mailer.SendMailInSeperateThread);
  caller.BeginInvoke(message, email.EmailId, null, null);
  // Never EndInvoke... 
  </code></pre>
  
  <p>Am I doing this right?</p>