title: Is there a way to catch exceptions using System.Net.Mail.SendAsync()
body: <p>I have a couple of methods already to send an email message syncronously.</p>
  
  <p>If an email fails, I use this fairly standard code:</p>
  
  <pre><code>    static void CheckExceptionAndResend(SmtpFailedRecipientsException ex, SmtpClient client, MailMessage message)
      {
          for (int i = 0; i &lt; ex.InnerExceptions.Length -1; i++)
          {
              var status = ex.InnerExceptions[i].StatusCode;
  
              if (status == SmtpStatusCode.MailboxBusy ||
                  status == SmtpStatusCode.MailboxUnavailable ||
                  status == SmtpStatusCode.TransactionFailed)
              {
                  System.Threading.Thread.Sleep(3000);
                  client.Send(message);
              }
          }
      }
  </code></pre>
  
  <p>However, I'm trying to achieve the same using SendAsync(). This is the code I have so far:</p>
  
  <pre><code>    public static void SendAsync(this MailMessage message)
      {
          message.ThrowNull("message");
  
          var client = new SmtpClient();
  
          // Set the methods that is called once the event ends
          client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
  
          // Unique identifier for this send operation
          string userState = Guid.NewGuid().ToString();
  
          client.SendAsync(message, userState);
  
          // Clean up
          message.Dispose();
      }
  
      static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
      {
          // Get the unique identifier for this operation.
          String token = (string)e.UserState;
  
          if (e.Error.IsNotNull())
          {
              // Do somtheing
          }
      }
  </code></pre>
  
  <p>The question is using token and/or e.Error how do I get the exception so that I can make the necessary checks for StatusCode and then resend?</p>
  
  <p>I've been Googling all afternoon but havn't found anything positive.</p>
  
  <p>Any advice appreciated.</p>