ASP.Net - SMTP SendMailAsync不工作

时间:2017-11-02 15:06:15

标签: asp.net asp.net-identity smtpclient sendasync

我有以下代码发送电子邮件但是我收到以下错误:

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

这是控制器中的代码:

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

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

导致此错误的原因是什么?请注意,应用程序正在防火墙之间运行?

0 个答案:

没有答案