2017-06-22 65 views
0

我有此代碼發送電子郵件來重置用戶密碼:電子郵件未與ASP.NET身份框架發送

// 
     // 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="[email protected]"> 
     <network host="mail.desytec.cl" password="xxxx" port="587" userName="[email protected]" enableSsl="false"/> 
     </smtp> 
    </mailSettings> 
    </system.net> 

順便說,讀一些職位的時候,我知道我必須有App_Start IdentityConfig.cs文件,但文件丟失。這可能是原因嗎?

+0

代碼中的哪個位置發送電子郵件?你在哪裏初始化smtp並使用smtp配置發送電子郵件? – ISHIDA

+0

https://stackoverflow.com/questions/40674457/how-to-configure-sender-email-credentials-for-asp-net-identity-usermanager-sende –

+0

我認爲這是由UserManager.SendEmailAsync自動調用 – jstuardo

回答

0

您可能已經得到了您的答案,但這是Google向我展示的第一篇文章,所以我正在回答它的完整性。

查看文件夾中的IdentityConfig.cs文件。你應該找到類似的代碼:

public class EmailService : IIdentityMessageService { 
    public Task SendAsync(IdentityMessage message) { 
     // Plug in your email service here to send an email. 
     return Task.FromResult(0); 
    } 
} 

我誤解了其他幾個帖子,並認爲我必須用上面的代碼創建一個新類。相反,您需要更新此方法以使用您要使用的任何電子郵件服務發送電子郵件。

我最終得到了類似的東西,但不完全一樣,所以你必須測試這是否真的適合你。

// code from https://stackoverflow.com/questions/40674457/how-to-configure-sender-email-credentials-for-asp-net-identity-usermanager-sende 
public async Task SendAsync(IdentityMessage message) { 
    // Plug in your email service here to send an email. 
    SmtpClient client = new SmtpClient(); 
    await client.SendMailAsync("email from web.config here", 
           message.Destination, 
           message.Subject, 
           message.Body); 
}