title: ASP.NET Identity Send Email: TaskCanceledException
body: <p>I'm trying to send verify emailadress email in asp.net identity 2.0 application but get the TaskCanceledException error.</p>
  
  <p>I tried whats in this thread: <a href="https://stackoverflow.com/questions/22797845/asp-net-identity-2-0-how-to-implement-iidentitymessageservice-to-do-async-smtp">Asp.Net Identity 2.0 - How to Implement IIdentityMessageService to do Async SMTP using SmtpClient?</a></p>
  
  <p>If i use the <code>await</code> the application just runs forever: no error message. but the second option returns the error.</p>
  
  <p>The error is thrown at: return <code>smtpClient.SendMailAsync(msg);</code></p>
  
  <p>IdentityConfig.cs</p>
  
  <pre><code>    public async Task SendAsync(IdentityMessage message)
      {
          // Plug in your email service here to send an email.
  
          try
          {
              #region formatter
              string text = string.Format("Please click on this link to {0}: {1}", message.Subject, message.Body);
              string html = "Please confirm your account by clicking this link: &lt;a href=\"" + message.Body + "\"&gt;link&lt;/a&gt;&lt;br/&gt;";
  
              html += HttpUtility.HtmlEncode(@"Or click on the copy the following link on the browser:" + message.Body);
              #endregion
  
              MailMessage msg = new MailMessage();
              msg.From = new MailAddress("");
              msg.To.Add(new MailAddress(message.Destination));
              msg.Subject = message.Subject;
              msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
              msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));
  
              using (var smtpClient = new SmtpClient())
              {
                  smtpClient.EnableSsl = true;
                  smtpClient.SendCompleted += (s, e) =&gt; { smtpClient.Dispose(); };
                  await smtpClient.SendMailAsync(msg);
              }
  
  
          }
          catch (Exception ex)
          {
  
              throw ex;
          }
  
  
      }
  </code></pre>
  
  <p>Register.aspx.cs</p>
  
  <pre><code>        protected void CreateUser_Click(object sender, EventArgs e)
      {
          var manager = Context.GetOwinContext().GetUserManager&lt;ApplicationUserManager&gt;();
          var user = new ApplicationUser()
          {
              UserName = Email.Text,
              Email = Email.Text,
              UserProfileInfo = new UserProfileInfo
              {
                  FirstName = Firstname.Text,
                  LastName = Lastname.Text,
                  Adress = Adress.Text,
                  Zip = Zip.Text,
                  City = City.Text,
                  Mobile = Mobile.Text
  
              }
  
          };
          IdentityResult result = manager.Create(user, Password.Text);
          if (result.Succeeded)
          {
              // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
              string code = manager.GenerateEmailConfirmationToken(user.Id);
              string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id, Request);
              manager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking &lt;a href=\"" + callbackUrl + "\"&gt;here&lt;/a&gt;.");
  
              manager.AddToRole(user.Id, "Konsument");
              IdentityHelper.SignIn(manager, user, isPersistent: false);
              IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
          }
          else
          {
              ErrorMessage.Text = result.Errors.FirstOrDefault();
          }
      }
  </code></pre>