I'm trying to send verify emailadress email in asp.net identity 2.0 application but get the TaskCanceledException error.
I tried whats in this thread: Asp.Net Identity 2.0 - How to Implement IIdentityMessageService to do Async SMTP using SmtpClient?
If i use the await
the application just runs forever: no error message. but the second option returns the error.
The error is thrown at: return smtpClient.SendMailAsync(msg);
IdentityConfig.cs
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: <a href=\"" + message.Body + "\">link</a><br/>";
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) => { smtpClient.Dispose(); };
await smtpClient.SendMailAsync(msg);
}
}
catch (Exception ex)
{
throw ex;
}
}
Register.aspx.cs
protected void CreateUser_Click(object sender, EventArgs e)
{
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
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 <a href=\"" + callbackUrl + "\">here</a>.");
manager.AddToRole(user.Id, "Konsument");
IdentityHelper.SignIn(manager, user, isPersistent: false);
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
ErrorMessage.Text = result.Errors.FirstOrDefault();
}
}