【问题标题】:My ForgotPassword method is not sending email我的 ForgotPassword 方法不发送电子邮件
【发布时间】:2016-08-28 18:13:41
【问题描述】:

我的忘记密码方法有一个问题,虽然它的状态=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);
        }

这是我的调试图像:

【问题讨论】:

  • 您如何配置您的电子邮件设置?你用什么smtp服务器?出于故障排除的目的,请尝试 UserManager.SendEmail( 而不是 Async 版本...这样更容易调试。
  • 对不起,我刚接触asp.net,我不知道怎么知道。你能告诉我吗?

标签: c# asp.net-mvc


【解决方案1】:

那么,您是否使用正确的 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的缺失,然后尝试调试您的应用程序,如果发生任何错误,您应该能够看到堆栈跟踪。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-30
    • 2011-09-16
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多