So for this article first we will create a new asp.net application and add the below form code into the page.
In above code I have created a form for forgot password. This have validated by using RequiredFieldValidator and RegularExpressionValidator.
public string SendPassword(string password, string emailId, string name) { try { MailMessage mail = new MailMessage(); mail.To.Add(emailId); mail.From = new MailAddress("youremail @gmail.com"); mail.Subject = "Your password for account " + emailId; string userMessage = ""; userMessage = userMessage + "<br/><b>Login Id:</b> " + emailId; userMessage = userMessage + "<br/><b>Passsword: </b>" + password; string Body = "Dear " + name + ", <br/><br/>Login detail for your account is a follows:<br/></br> " + userMessage + "<br/><br/>Thanks"; mail.Body = Body; mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; //SMTP Server Address of gmail smtp.Port = 587; smtp.Credentials = new System.Net.NetworkCredential("youremail@gmail.com", "password"); // Smtp Email ID and Password For authentication smtp.EnableSsl = true; smtp.Send(mail); return "Please check your email for account login detail."; } catch { return "Error............"; } } |
public DataTable GetData(string emailId) { DataTable dt = new DataTable(); SqlConnection con = new SqlConnection("Your connection string"); SqlDataAdapter da = new SqlDataAdapter("Select * from Table where EmailId='" + emailId + "';", con); con.Open(); da.Fill(dt); con.Close(); return dt; } |
Now we will check he click event of the button to manage the forgot password.
protected void Button1_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); dt = GetData(TextBox1.Text); if (dt.Rows.Count > 0) { //Send login detail to user email string password = dt.Rows[0]["Name"].ToString(); string emailId = dt.Rows[0]["EmailId"].ToString(); string name = dt.Rows[0]["Password"].ToString(); string status = SendPassword(password, emailId, name); Label1.Text = status; } else { //if email id is not register with us Label1.Text = "Please provide a valid register email id."; } } |
Now check the complete code.
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Net.Mail; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; namespace WebApplication7 { public partial class Forgotpassword : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); dt = GetData(TextBox1.Text); if (dt.Rows.Count > 0) { //Send login detail to user email string password = dt.Rows[0]["Name"].ToString(); string emailId = dt.Rows[0]["EmailId"].ToString(); string name = dt.Rows[0]["Password"].ToString(); string status = SendPassword(password, emailId, name); Label1.Text = status; } else { //if email id is not register with us Label1.Text = "Please provide a valid register email id."; } } public DataTable GetData(string emailId) { DataTable dt = new DataTable(); SqlConnection con = new SqlConnection("Your connection string"); SqlDataAdapter da = new SqlDataAdapter("Select * from Table where EmailId='" + emailId + "';", con); con.Open(); da.Fill(dt); con.Close(); return dt; } public string SendPassword(string password, string emailId, string name) { try { MailMessage mail = new MailMessage(); mail.To.Add(emailId); mail.From = new MailAddress("youremail@gmail.com"); mail.Subject = "Your password for account " + emailId; string userMessage = ""; userMessage = userMessage + "<br/><b>Login Id:</b> " + emailId; userMessage = userMessage + "<br/><b>Passsword: </b>" + password; string Body = "Dear " + name + ", <br/><br/>Login detail for your account is a follows:<br/></br> " + userMessage + "<br/><br/>Thanks"; mail.Body = Body; mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; //SMTP Server Address of gmail smtp.Port = 587; smtp.Credentials = new System.Net.NetworkCredential("youremail@gmail.com", "Password"); // Smtp Email ID and Password For authentication smtp.EnableSsl = true; smtp.Send(mail); return "Please check your email for account login detail."; } catch { return "Error............"; } } } } |
0 comments:
Please let me know your view