0

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?

adam78
  • 9,668
  • 24
  • 96
  • 207
  • Your `SendAsync` method exits *before* the email is sent. That means that `SmptClient` gets garbage collected before the sending operation completes. Change the signature to `async Task` and the last line to `await smptClient.SendMailAsync(mailMessage);`. You don't need to return anything – Panagiotis Kanavos Nov 02 '17 at 15:29
  • @PanagiotisKanavos the controller action already has `async Task` so why do I need it again on the method? In addition I already have `await` on the method call inside the controller so why do I need it again in the actual method? By the way the code is taken from the Mircrosoft site. – adam78 Nov 02 '17 at 15:56
  • I'm talking about `EmailService.SendAsync` which *doesn't*. Where did you find the code anyway? Code examples can have errors too. – Panagiotis Kanavos Nov 02 '17 at 16:08
  • 1
    Possible duplicate of [Asp.Net Identity 2.0 - How to Implement IIdentityMessageService to do Async SMTP using SmtpClient?](https://stackoverflow.com/questions/22797845/asp-net-identity-2-0-how-to-implement-iidentitymessageservice-to-do-async-smtp) – Panagiotis Kanavos Nov 02 '17 at 16:11
  • @PanagiotisKanavos having made the changes you suggested have made no difference either. – adam78 Nov 02 '17 at 16:26
  • @PanagiotisKanavos please can you show me with some code where you actually mean I should make the change? – adam78 Nov 02 '17 at 16:27
  • Did you read the duplicate? It shows exactly where you should make the change. In your `EmailService.SendAsync` method, change the signature from `Task` to `async Task` and instead of `return smtpClient.SendMailAsync(mailMessage);` write `await smtpClient.SendMailAsync(mailMessage);` – Panagiotis Kanavos Nov 02 '17 at 16:40

0 Answers0