I have the following code to send email but I get the following error:
An asynchronous module or handler completed while an asynchronous operation was still pending
This is the code in controller:
[AllowAnonymous]
public async Task<ActionResult> GenerateEmailConfirmation(ApplicationUser user)
{
string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Action(
"ConfirmEmail",
"Account",
new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(
user.Id,
"Some subject",
"This the message...<a href=\"" + callbackUrl + "\">here</a>");
return View("CheckEmail");
}
This is the code in identityconfig:
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
var smtpClient = new SmtpClient
{
DeliveryMethod = SmtpDeliveryMethod.Network,
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("jxxx@gmail.com", "xxx")
};
var mailMessage = new MailMessage("jxxx@gmail.com", message.Destination)
{
Subject = message.Subject,
Body = message.Body,
IsBodyHtml = true
};
return smtpClient.SendMailAsync(mailMessage);
}
}
What is causing this error? Note the application is being run between a firewall?