【问题标题】:MVC ForgotPassword method not sending reset emailMVC ForgotPassword 方法不发送重置电子邮件
【发布时间】:2017-05-15 08:52:55
【问题描述】:

我的 AccountController 中有以下 GET 和 POST 方法:

// GET: /Account/ForgotPassword
        [AllowAnonymous]
        public ActionResult ForgotPassword()
        {
            return View();
        }

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

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

这使用 Visual Studio 定义的标准 ForgotPassword 方法。我只是在我的登录视图上放了一个操作链接,如下所示:

                <p>
                    @Html.ActionLink("Forgot your password?", "ForgotPassword")
                </p>

问题是,在 ForgotPassword 视图中提交要重置的电子邮件后,我没有收到注册用户的重置电子邮件。我的代码有什么问题吗?

我也像这样在我的 Web.config 中配置了邮件设置,所以这应该不是问题:

 <mailSettings>
  <smtp from="MyEmail">
    <network host="smtp.gmail.com" port="587" userName="MyEmail" password="MyEmailPassword" enableSsl="true" />

  </smtp>
</mailSettings>

我尝试过回答 Stackoverflow 上的类似问题,比如换行:

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

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

但这没有用。我还允许低安全性应用程序向我的帐户发送电子邮件。我还尝试在我的 smtp 标签下设置deliveryMethod="Network",但这也没有帮助。

没有收到重置邮件可能是什么问题?

【问题讨论】:

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


    【解决方案1】:

    只有在您授予低安全性应用程序的访问权限并启用电子邮件转发协议后,Gmail smtp 才会发送电子邮件。

    启用电子邮件转发 (POP) 后,您应该会收到来自 google 的电子邮件,说有人试图发送类似消息的电子邮件,并且您可以从邮件中启用低安全性应用程序以通过 gmail 发送电子邮件。

    【讨论】:

    • 是的,我知道。该选项已启用。我也没有再收到那封电子邮件,因为它已经在我之前实现的网站上启用了一项功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 2011-02-14
    • 1970-01-01
    • 2013-12-18
    • 2015-08-13
    • 2020-09-19
    • 2017-11-01
    相关资源
    最近更新 更多