Forgot Password By Email Page Code In Asp.Net
This Example explains how to Create Forgot Password By Email Page Code In Asp.Net Using C# And VB.NET.
I have placed one textbox and button on the ForgotPassword.aspxpage to send mail to email id stored in database.
You can also send Reset Password Link instead of sending Username password in the email.
HTML SOURCE OF FORGOT PASSWORD PAGE
Write following code in Click Event of Button to retrieve username and password associated with EmailID provided by user from database and send the information to this email id.
C# CODE
I have placed one textbox and button on the ForgotPassword.aspxpage to send mail to email id stored in database.
You can also send Reset Password Link instead of sending Username password in the email.

HTML SOURCE OF FORGOT PASSWORD PAGE
<form id="Form1" runat="server">
<div>
<fieldset>
<legend>Forgot Password</legend>
<asp:Label ID="lblEmail" runat="server" Text="Email Address: "/>
<asp:TextBox ID="txtEmail" runat="server"/>
<asp:RequiredFieldValidator ID="RV1" runat="server"
ControlToValidate="txtEmail"
ErrorMessage="Please Enter EmailID"
SetFocusOnError="True">*
</asp:RequiredFieldValidator>
<asp:Button ID="btnPass" runat="server" Text="Submit"
onclick="btnPass_Click"/>
<asp:ValidationSummary ID="ValidationSummary1"
runat="server" CssClass="error"/>
<asp:Label ID="lblMessage" runat="server" Text=""/>
</fieldset>
</div>
</form>
Write following code in Click Event of Button to retrieve username and password associated with EmailID provided by user from database and send the information to this email id.
C# CODE
using System;using System.Data.SqlClient;using System.Configuration;using System.Data;using System.Net.Mail;protected void btnPass_Click(object sender, EventArgs e) { //Create Connection String And SQL Statement string strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; string strSelect = "SELECT UserName,Password FROM Users WHERE Email = @Email"; SqlConnection connection = newSqlConnection(strConnection); SqlCommand command = new SqlCommand(); command.Connection = connection; command.CommandType = CommandType.Text; command.CommandText = strSelect; SqlParameter email = newSqlParameter("@Email", SqlDbType.VarChar, 50); email.Value = txtEmail.Text.Trim().ToString(); command.Parameters.Add(email); //Create Dataset to store results and DataAdapter to fill Dataset DataSet dsPwd = new DataSet(); SqlDataAdapter dAdapter = newSqlDataAdapter(command); connection.Open(); dAdapter.Fill(dsPwd); connection.Close();if(dsPwd.Tables[0].Rows.Count > 0 ){MailMessage loginInfo = newMailMessage();loginInfo.To.Add(txtEmail.Text.ToString());loginInfo.From = newMailAddress("YourID@gmail.com");loginInfo.Subject = "Forgot Password Information";loginInfo.Body = "Username: " + dsPwd.Tables[0].Rows[0]["UserName"] + "<br><br>Password: " + dsPwd.Tables[0].Rows[0]["Password"] + "<br><br>";loginInfo.IsBodyHtml = true;SmtpClient smtp = new SmtpClient();smtp.Host = "smtp.gmail.com";smtp.Port = 587;smtp.EnableSsl = true;smtp.Credentials = newSystem.Net.NetworkCredential("YourGmailID@gmail.com","YourGmailPassword");smtp.Send(loginInfo);lblMessage.Text = "Password is sent to you email id,you can now <a href="Login.aspx">Login</a>";}else{lblMessage.Text = "Email Address Not Registered";}}
No comments:
Post a Comment