我知道这个问题已被提出(SendMailAsync : An asynchronous module or handler completed while an asynchronous operation was still pending )但我显然仍然做错了,因为我仍然收到以下错误:
异步操作时完成的异步模块或处理程序 还在等待。
这是我的控制器代码:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindByNameAsync(model.Email);
if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
{
bool isApproved = IsOrganizationForMemberApproved(user);
if (!isApproved)
{
ModelState.AddModelError("", "Your organization has not been approved.");
return View(model);
}
// Don't reveal that the user does not exist or is not confirmed
return View("ForgotPasswordConfirmation");
}
await CustomEmailService.SendPasswordReset(this, UserManager, Request, user);
return RedirectToAction("ForgotPasswordConfirmation", "Account");
}
// If we got this far, something failed, redisplay form
return View(model);
}
这是我的电子邮件助手功能:
public static async Task SendPasswordReset(Controller controller,
ApplicationUserManager userManager,
HttpRequestBase request,
ApplicationUser user)
{
string code = await userManager.GeneratePasswordResetTokenAsync(user.Id);
var callbackUrl = controller.Url.Action("ResetPassword", "Account",
new { userId = user.Id, code = code }, protocol: request.Url.Scheme);
var message = LoadPasswordResetMessage(callbackUrl);
using (EmailManager emailManager = new EmailManager())
{
await emailManager.SendEmailAsync(user.Email,
user.UserName, "Reset Password", message);
}
}
最后负责发送电子邮件的函数:
public async Task SendEmailAsync(string toEmailAddress,
toDisplayName, string subject, string htmlContent)
{
//... read settings
var message = new MailMessage();
message.To.Add(new MailAddress(toEmailAddress, toDisplayName));
message.From = new MailAddress(fromEmailAddress, fromDisplayName);
message.Subject = subject;
message.Body = htmlContent;
message.IsBodyHtml = true;
using (var smtp = new SmtpClient())
{
var credential = new NetworkCredential
{
UserName = smtpUserName,
Password = smtpPassword
};
smtp.Credentials = credential;
smtp.Host = smtpHost;
smtp.Port = Convert.ToInt32(smtpPort);
smtp.EnableSsl = Convert.ToBoolean(enableSsl);
await smtp.SendMailAsync(message);
}
}
我错过了什么?任何帮助将不胜感激。
感谢。