4

I know this question has already been asked (SendMailAsync : An asynchronous module or handler completed while an asynchronous operation was still pending ) but I'm obviously still doing something wrong as I'm still getting the following error:

An asynchronous module or handler completed while an asynchronous operation was still pending.

This is my controller's code:

[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);
}

And this is my email helper function:

    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);
        }
    }

and finally the function responsible for sending the email:

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);
    }
}

What am I missing? Any help would be greatly appreciated.

Thanks.

Thierry
  • 6,142
  • 13
  • 66
  • 117
  • 2
    What is the error? – Zahid Mustafa Aug 29 '17 at 04:35
  • The error means that there's an async method somewhere that you're not awaiting. Not sure where, but if you search the `async` keyword in your code it shouldn't be too hard to find – Kevin Gosse Aug 29 '17 at 06:48
  • @KevinGosse if you look at the 3 functions I've posted, all of them have the async keyword. This is why it's been driving me nut! The weird part is that this is only occurring inconsistently and it's only occurring when using a specific smtp host server name and when used with another smtp host server name, it never happens. I'll keep looking anyway. Thanks. – Thierry Aug 31 '17 at 00:53

0 Answers0