locked
Forgot Password Email RRS feed

  • Question

  • User-351779185 posted

    Hello all,

    I am having problems sending an email when a user has forgotten their password.

    After entering their email address and clicking on the submit button their are redirected to the ForgortPaswordConfirmation page which is great. The problem is that no email is sent.

    I am able to send a 'Test Email' with no problems. I am using Mailkit with gmail in VS2019.

    Could someone kindly have a look at my coding and advise where I am going wrong?

    //Forgot Password
            [HttpGet]
            [AllowAnonymous]
            public IActionResult ForgotPassword()
            {
                return View();
            }
    
            [HttpPost]
            [AllowAnonymous]
            public async Task<IActionResult> ForgotPassword(ForgotPasswordViewModel model)
            {
                if (ModelState.IsValid)
                    return View("ForgotPasswordConfirmation");
                {
                    // Find the user by email
                    var user = await userManager.FindByEmailAsync(model.Email);
                    // If the user is found
                    if (user == null)
                        return RedirectToAction("ForgotPasswordConfirmation");
                    {
                        // Generate the reset password token
                        var token = await userManager.GeneratePasswordResetTokenAsync(user);
    
                        // Build the password reset link
                        var callback = Url.Action(nameof(ResetPassword), (nameof(AccountController), new { token, email = user.Email }, Request.Scheme));
    
                        //Build & send the email
                        var message = new Message(new string[] { "sendingemailaddress@gmail.com" }, "Reset password token", callback, null);
                        await emailSender.SendEmailAsync(message);
    
                        // Send the user to Forgot Password Confirmation view
                        return RedirectToAction("ForgotPasswordConfirmation");
                    }
                }
            }

    Many thanks

    Friday, June 26, 2020 8:48 AM

All replies

  • User475983607 posted

    You did not provide the emailSender service source code which is responsible for sending the email. 

    Set a break point and step through your code to check for failures.  Also login in to your gmail account and look for security warnings.

    Friday, June 26, 2020 12:03 PM
  • User-351779185 posted

    Hi mgebhard,

    Many thanks for your reply.

    Is this not linking to the source code?

    I forgot to mention that I am new to all this and this is my first app so if possible would you be willing to show me what code is required.

            private readonly IEmailSender emailSender;
    
            public AccountController(IEmailSender emailSender)
            {
                this.emailSender = emailSender;

    Friday, June 26, 2020 1:01 PM