1
Reply

How send password to authenticate user

Sk Jha

Sk Jha

Dec 22 2017 11:39 PM
139
How to make forget password where i want to recover password by email.
 
here is my code but its not working.
  1. Connection c = new Connection();  
  2.    protected void btnPassword_Click(object sender, EventArgs e)  
  3.    {  
  4.        string userId = "";  
  5.        string email = "";  
  6.        string password ="";  
  7.          
  8.        using (SqlConnection con = new SqlConnection(c.GetConnectionString()))  
  9.        {  
  10.            using (SqlCommand cmd = new SqlCommand("SELECT * FROM User_login WHERE userId = @user"))  
  11.            {  
  12.                cmd.Parameters.AddWithValue("@user", txtUser.Text.Trim());  
  13.                cmd.Connection = con;  
  14.                con.Open();  
  15.                using (SqlDataReader dr = cmd.ExecuteReader())  
  16.                {  
  17.                    if (dr.Read())  
  18.                    {  
  19.                        userId = dr["userId"].ToString();  
  20.                        email = dr["email"].ToString();  
  21.                        password = dr["Password"].ToString();  
  22.                    }  
  23.                }  
  24.                con.Close();  
  25.            }  
  26.        }  
  27.        if (!string.IsNullOrEmpty(password))  
  28.        {  
  29.            MailMessage mm = new MailMessage("[email protected]",email);  
  30.            mm.Subject = "Password Recovery";  
  31.            mm.Body = string.Format("Hi {0},<br /><br />Your password is {1}.<br /><br />Thank You.", userId, password);  
  32.            mm.IsBodyHtml = true;  
  33.            SmtpClient smtp = new SmtpClient();  
  34.            smtp.Host = "smtp.gmail.com";  
  35.            smtp.EnableSsl = true;  
  36.            NetworkCredential NetworkCred = new NetworkCredential();  
  37.            NetworkCred.UserName = "[email protected]";  
  38.            NetworkCred.Password = "*****";  
  39.            smtp.UseDefaultCredentials = true;  
  40.            smtp.Credentials = NetworkCred;  
  41.            smtp.Port = 587;  
  42.            smtp.Send(mm);  
  43.            er.Visible = true;  
  44.            ltrError.Text = "Password has been sent to your email address.";  
  45.        }  
  46.        else  
  47.        {  
  48.            er.Visible = true;  
  49.            ltrError.Text = "This email address does not match our records.";  
  50.        }  
  51.    }  
 

Answers (1)