我的ForgotPassword方法未发送电子邮件

时间:2015-07-11 08:17:43

标签: c# asp.net-mvc

我的忘记密码方法有问题,虽然它的状态= RantoCompletion但是根本没有收到任何电子邮件。我尝试了很多电子邮件地址,但没有任何内容。谁能告诉我可能是什么问题。 这是代码:

   // POST: /Account/ForgotPassword
        [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)))
                {
                    // Don't reveal that the user does not exist or is not confirmed
                    return View("ForgotPasswordConfirmation");
                }

                string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
                var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
                return RedirectToAction("ForgotPasswordConfirmation", "Account");
            }

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

这是我的调试图片: an image of my debugging

1 个答案:

答案 0 :(得分:0)

那么,您是否使用正确的smtp服务器设置配置了Web.Config?请参阅此讨论:GMail + C# + Web.Config: Send Mail Works Programmatically, Throws Exception Using Web.Config Values

您需要设置的是:

<system.net>
  <mailSettings>
    <smtp deliveryMethod="Network" from="your@emailaddress.com">
        <network host="smtp.gmail.com" port="587" defaultCredentials="true" userName="your@emailaddress.com" password="somepassword" />
    </smtp>
  </mailSettings>
</system.net>

之后,尝试更改此行:

await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");

对此:

UserManager.SendEmail(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");

注意缺少Asyncawait,然后尝试调试应用程序,如果发生任何错误,您应该能够看到堆栈跟踪。