ASP MVC 5电子邮件确认

时间:2016-12-20 07:07:18

标签: c# asp.net asp.net-mvc email

我正在尝试在我的MVC 5应用上启用电子邮件确认。

这是我的代码,因为我一直收到此错误:

  

[InvalidOperationException:在异步操作仍处于挂起状态时完成异步模块或处理程序。]

这是我的注册行动:

public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser
            {
                UserName = model.Email,
                Email = model.Email,
                First = model.First,
                Last = model.Last
            };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                UserManager.AddToRole(user.Id, "Default");

                await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                // Send an email with this link
                 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, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                return View("DisplayEmail");
                //return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

这是电子邮件部分

 public class EmailService : IIdentityMessageService
{
    public Task SendAsync(IdentityMessage message)
    {
        var emailManager = new EmailConfig(EmailConfig.EmailSystem, message.Destination, message.Subject, message.Body);
        return emailManager.Send();
        // Plug in your email service here to send an email.
    }
}

这是电子邮件实际发送的地方

 public Task Send()
    {
        foreach(var mail in this.mails)
        {
            this.client.SendMailAsync(mail);
        }
        return Task.FromResult(0);
    }

0 个答案:

没有答案