2017-06-22 4 views
0

は、私は、ユーザーのパスワードをリセットするために電子メールを送信するには、このコードを持っています:Eメールが

// 
     // 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ファイルをApp_Startフォルダ内に表示します。

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); 
} 
関連する問題