【发布时间】: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