How to make forget password where i want to recover password by email.
here is my code but its not working.
- Connection c = new Connection();
- protected void btnPassword_Click(object sender, EventArgs e)
- {
- string userId = "";
- string email = "";
- string password ="";
-
- using (SqlConnection con = new SqlConnection(c.GetConnectionString()))
- {
- using (SqlCommand cmd = new SqlCommand("SELECT * FROM User_login WHERE userId = @user"))
- {
- cmd.Parameters.AddWithValue("@user", txtUser.Text.Trim());
- cmd.Connection = con;
- con.Open();
- using (SqlDataReader dr = cmd.ExecuteReader())
- {
- if (dr.Read())
- {
- userId = dr["userId"].ToString();
- email = dr["email"].ToString();
- password = dr["Password"].ToString();
- }
- }
- con.Close();
- }
- }
- if (!string.IsNullOrEmpty(password))
- {
- MailMessage mm = new MailMessage("[email protected]",email);
- mm.Subject = "Password Recovery";
- mm.Body = string.Format("Hi {0},<br /><br />Your password is {1}.<br /><br />Thank You.", userId, password);
- mm.IsBodyHtml = true;
- SmtpClient smtp = new SmtpClient();
- smtp.Host = "smtp.gmail.com";
- smtp.EnableSsl = true;
- NetworkCredential NetworkCred = new NetworkCredential();
- NetworkCred.UserName = "[email protected]";
- NetworkCred.Password = "*****";
- smtp.UseDefaultCredentials = true;
- smtp.Credentials = NetworkCred;
- smtp.Port = 587;
- smtp.Send(mm);
- er.Visible = true;
- ltrError.Text = "Password has been sent to your email address.";
- }
- else
- {
- er.Visible = true;
- ltrError.Text = "This email address does not match our records.";
- }
- }