5.7.0 Authentication Required. Learn more

I am trying to reset a user’s password by sending an email to the user with a link to reset the password.

The following code is in the btnResetPwd_Click() event:

    protected void btnResetPwd_Click(object sender, EventArgs e)
    {
        string emailAddress = txtEmail.Text;

        User u = db.Users.Single(x => x.EmailAddress == emailAddress);


        if (u != null)
        {
            lblMessage.ForeColor = System.Drawing.Color.LimeGreen;
            MailMessage mailMessage = new MailMessage();

            StringBuilder sbEmailBody = new StringBuilder();
            sbEmailBody.Append("Dear " + u.Name + ",<br/><br/>");
            sbEmailBody.Append("Please click on the following link to reset your password");
            sbEmailBody.Append("<br/>"); sbEmailBody.Append("http://localhost/Assignment/Registration/ChangePwd.aspx?uid=" +u.Id);
            sbEmailBody.Append("<br/><br/>");
            sbEmailBody.Append("<b>Pragim Technologies</b>");

            mailMessage.IsBodyHtml = true;

            mailMessage.Body = sbEmailBody.ToString();
            mailMessage.Subject = "Reset Your Password";
            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);

            smtpClient.Credentials = new System.Net.NetworkCredential()
            {
                UserName = "YourEmail@gmail.com",
                Password = "YourPassword"
            };
            string to = u.EmailAddress;
            string from = "potato@gmail.com";

            smtpClient.EnableSsl = true;

            mailMessage.From = new MailAddress(from);
            mailMessage.To.Add(to);
            smtpClient.Send(mailMessage);
            smtpClient.UseDefaultCredentials = false;

            lblMessage.Text = "An email with instructions to reset your password is sent to your registered email";


        }
        else
        {
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = "Email Address not found!";
        }

    }

The following is in the web.config:

        <system.net>
        <mailSettings>
        <smtp from="Admin &lt;potato@gmail.com&gt;">
        <network host="smt.gmail.com"
                 port="587"
             enableSsl="true"
             userName="potato@gmail.com"
             password="password"/>
              </smtp>
             </mailSettings>
             </system.net>

I am getting an error when I run the above code.

I am trying to reset a user’s password by sending an email containing a link to a password reset page. The btnResetPwd_Click() event attempts to find the user’s email address in the database and then sends an email with the instructions using the SMTP credentials from web.config. If the user is not found, an error message is displayed.

The error is likely due to a typo in the web.config file. The host attribute in the network element should be smtp.gmail.com instead of smt.gmail.com.