【发布时间】:2017-06-22 01:10:34
【问题描述】:
我正在使用此代码发送电子邮件以重置用户密码:
//
// POST: /Account/ForgotPassword
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<JsonResult> ForgotPassword(ForgotPasswordViewModel model, bool CaptchaValid)
{
string mensaje = String.Empty;
if (ModelState.IsValid)
{
if (!CaptchaValid)
mensaje = "ERROR: Captcha inválido.";
else
{
var user = await UserManager.FindByEmailAsync(model.Email);
if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
{
mensaje = "ERROR: La dirección de correo electrónico no está registrada.";
}
else
{
var provider = new DpapiDataProtectionProvider("WebAttendance");
UserManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser, string>(provider.Create("UserToken")) as IUserTokenProvider<ApplicationUser, string>;
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", "Por favor, cambie su contraseña al hacer click <a href=\"" + callbackUrl + "\">aquí</a>");
return Json("INFO: Se envió un mail a su cuenta de correo con instrucciones para cambiar su contraseña.");
}
}
}
// If we got this far, something failed, redisplay form
return Json(mensaje);
}
上面的代码运行没有任何错误,但实际上并没有发送电子邮件。
这是 Web.config 条目:
<system.net>
<mailSettings>
<smtp from="info@xxx.com">
<network host="mail.desytec.cl" password="xxxx" port="587" userName="yyy@xxx.cl" enableSsl="false"/>
</smtp>
</mailSettings>
</system.net>
顺便说一句,在阅读一些帖子时,我知道我必须在 App_Start 中有 IdentityConfig.cs 文件,但该文件丢失了。可能是这个原因吗?
【问题讨论】:
-
您在代码中的哪个位置发送电子邮件?你在哪里初始化 smtp 并使用 smtp 配置发送电子邮件?
-
我认为这是由 UserManager.SendEmailAsync 调用自动完成的
-
@YuriS 我以前读过。看看我写的关于 IdentityConfig.cs 的内容。但是,我已经使用该帖子中显示的代码手动添加了该文件,但它不起作用。
-
如果您没有该文件,则意味着您没有正确创建该项目。仅仅添加它是不够的。有一些代码设置。例如在 public static ApplicationUserManager Create(IdentityFactoryOptions
options, IOwinContext context) 应该调用 var manager = new ApplicationUserManager(new UserStore (context.Get ()));然后 manager.EmailService = new EmailService();我会正确地重新创建项目
标签: c# asp.net email asp.net-identity