Guest User

Untitled

a guest
Jan 19th, 2019
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. public partial class ForgotPassword : System.Web.UI.Page
  2. {
  3. protected void Page_Load(object sender, EventArgs e)
  4. {
  5.  
  6. }
  7. protected void btnPass_Click(object sender, EventArgs e)
  8. {
  9. //Create Connection String And SQL Statement
  10. string strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
  11. string strSelect = "SELECT UserName,Password FROM Users WHERE Email = @Email";
  12.  
  13. SqlConnection connection = new SqlConnection(strConnection);
  14. SqlCommand command = new SqlCommand();
  15. command.Connection = connection;
  16. command.CommandType = CommandType.Text;
  17. command.CommandText = strSelect;
  18.  
  19. SqlParameter email = new SqlParameter("@Email", SqlDbType.VarChar, 50);
  20. email.Value = txtEmail.Text.Trim().ToString();
  21. command.Parameters.Add(email);
  22.  
  23. //Create Dataset to store results and DataAdapter to fill Dataset
  24. DataSet dsPwd = new DataSet();
  25. SqlDataAdapter dAdapter = new SqlDataAdapter(command);
  26. connection.Open();
  27. dAdapter.Fill(dsPwd);
  28. connection.Close();
  29. if(dsPwd.Tables[0].Rows.Count > 0 )
  30. {
  31.  
  32. MailMessage loginInfo = new MailMessage();
  33. loginInfo.To.Add(txtEmail.Text.ToString());
  34. loginInfo.From = new MailAddress("YourID@gmail.com");
  35. loginInfo.Subject = "Forgot Password Information";
  36.  
  37. loginInfo.Body = "Username: " + dsPwd.Tables[0].Rows[0]["UserName"] + "<br /><br />Password: " + dsPwd.Tables[0].Rows[0]["Password"] + "<br /><br />";
  38. loginInfo.IsBodyHtml = true;
  39. SmtpClient smtp = new SmtpClient();
  40. smtp.Host = "smtp.gmail.com";
  41. smtp.Port = 587;
  42. smtp.EnableSsl = true;
  43. smtp.Credentials = new System.Net.NetworkCredential("YourGmailID@gmail.com", "YourGmailPassword");
  44. smtp.Send(loginInfo);
  45. lblMessage.Text = "Password is sent to you email id,you can now <a href='Login.aspx'>Login</a>";
  46. }
  47. else
  48. {
  49. lblMessage.Text = "Email Address Not Registered";
  50. }
  51.  
  52. }
  53. }
  54.  
  55. smtp.Send(loginInfo);
  56.  
  57. try
  58. {
  59. smtp.Send(loginInfo);
  60. }
  61. catch (Exception ex)
  62. {
  63. //Handle your exception here
  64. lblMessage.Text = "Oeps, something when wrong when we tried to send the email";
  65. return;
  66. }
  67.  
  68. Outgoing Mail (SMTP) Server - requires TLS3 or SSL: smtp.gmail.com (use authentication)
  69. Use Authentication: Yes
  70. Port for TLS/STARTTLS: 587
  71. Port for SSL: 465
Add Comment
Please, Sign In to add comment