title: Unit Test That Sends Email and Creates a new Thread
body: <p>I really am feeling bad.</p>
  
  <p>I have one web application that sends Emails for notifications.</p>
  
  <p>It is a class that contains a basic method that sends a email like this:</p>
  
  <pre><code> MailMessage email = new MailMessage();
  
          email.To.Add("xxx@gmail.com");
  
          email.Subject = "Test";
          email.Body = "t";
  
          SmtpClient client = new SmtpClient();
  
          client.Send(email);
  </code></pre>
  
  <p>But i have a service called NotificatorService that verify some logic, and because this is just a "extra" the method for this service is called in a new Thread.. What i do is this:</p>
  
  <pre><code>public void NotifyMembers(NotificatorClientDTO clientDto) {
  if(clientDto == null)
      throw new ArgumentNullException("clientDto");
  
  
  Thread t = new Thread( 
      () =&gt; {
      // go to database, verify business rules, etc...
  
      // At the end if it is ok, invoke the method that sends a email
  }
  
  t.Start()
  </code></pre>
  
  <p>}</p>
  
  <p>If i create a unit test that instantiates the NotificatorService and call notifyMembers with some data, the code will throw an exception but only if i execute in a seperated thread.</p>
  
  <p>The thread that i create is setted at foregronnd so the system don't destroy my process i suppose.</p>
  
  <p>THe exception message is this:</p>
  
  <p>SmtpException: Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack</p>
  
  <p>I walk to 2 inner exceptions and i see this</p>
  
  <p>_message    "Thread was being aborted." string</p>
  
  <p>Why this is happening?
  What causes the thread to be aborted?</p>