title: Asp.Net Identity 2.0 - How to Implement IIdentityMessageService to do Async SMTP using SmtpClient?
body: <p>I've implemented a simple EmailService for Asp.Net Identity 2.0 (via the <code>IIdentityMessageService</code> interface.</p>
  
  <pre><code>    public class EmailService : IIdentityMessageService
  {
      public Task SendAsync(IdentityMessage message)
      {
          // convert IdentityMessage to a MailMessage
          var email = 
             new MailMessage(new MailAddress("noreply@mydomain.com", "(do not reply)"), 
             new MailAddress(message.Destination))
          {
              Subject = message.Subject,
              Body = message.Body,
              IsBodyHtml = true
          };
  
          using (var client = new SmtpClient()) // SmtpClient configuration comes from config file
          {
              return client.SendMailAsync(email);
          }
      }
  }
  </code></pre>
  
  <p>To send an email, I go through UserManager:</p>
  
  <p><code>await _userManager.SendEmailAsync(user.Id, "Confirm your account","Please confirm your account by clicking this link: &lt;a href=\"" + callbackUrl + "\"&gt;link&lt;/a&gt;");</code></p>
  
  <p>The problem is that I get a <code>System.Threading.Tasks.TaskCanceledException</code> when I call <code>SendEmailAsync()</code> and it's not clear why. </p>
  
  <p>If I send email synchronously (<code>client.Send(email)</code>), everything works fine. </p>
  
  <p>So my questions are:</p>
  
  <ul>
  <li><p>How do I prevent the TaskCanceledException from getting thrown?
  and (assuming I can overcome this error),</p></li>
  <li><p>How should I go about communicating errors during email sending back to the client (i.e., "no such user here" type responses from the SmtpClient?</p></li>
  </ul>