title: SmtpClient SendAsync error
body: <p><strong>BACKGROUND</strong></p>
  
  <p>I have written a little console application that monitors a RabbitMQ queue for emails. Whenever an email is pushed onto the queue, my application would pick up that email, process it and send it.</p>
  
  <p><strong>CODE</strong></p>
  
  <p>Below is the code for my email service, it is what actually sends out the email.</p>
  
  <pre><code>public class MailService : IMailService
  {
      private readonly SmtpClient _smtpClient = new SmtpClient();
  
      public void SendEmail(string toEmail, string subject, string body, bool isHtml)
      {
        var emailMessage = BuildEmailMessage(toEmail.Trim(), subject.Trim(), body, isHtml);
        _smtpClient.SendAsync(emailMessage, null);
      }
  
      #region Helpers
      private static MailMessage BuildEmailMessage(string toEmail, string subject, string body, bool isHtml)
      {
        const string fromEmailAddress = "james.brown@world.com";
        var emailMessage = new MailMessage(fromEmailAddress, toEmail, subject, body) { IsBodyHtml = isHtml };
        return emailMessage;
      }
      #endregion
  }
  </code></pre>
  
  <p><strong>PROBLEM</strong></p>
  
  <p>I had 2 emails on the RabbitMQ queue, and when I fired up my console application consumer, it threw the following exception after sending the first email (I received that 1st email in my inbox).</p>
  
  <blockquote>
    <p>An asynchronous call is already in progress. It must be completed or canceled before you can call this method.</p>
  </blockquote>
  
  <p>I did some digging around, and saw <a href="https://stackoverflow.com/questions/8369614/is-it-wise-to-use-smtp-sendasync-in-asp-net">this thread</a> which explains why I got this. Apparently using <code>SendAsync()</code> isn't the way to go because:</p>
  
  <blockquote>
    <p>After calling SendAsync, you must wait for the e-mail transmission to complete before attempting to send another e-mail message using Send or SendAsync.</p>
  </blockquote>
  
  <p>So what is the recommend way of going about this? I can create a new instance of the <code>SmtpClient</code> class for each email, but is that really a good idea?</p>