Send Forgotten Password and Username in Email in ASP.Net C#

In this tutorial we have learn Send Forgotten Password and Username in Email in ASP.Net C# Using the Forgot Password page, user will be allowed to recover the password using his Email address and then the recovered password will be sent to his registered email address

We have a login panel and account page and a forgotten password link, the user can login with his account details, but if he forgets the password and username he/she can access the username and password via the forgotten password link, in that they must feed their email and username and password will be sent to their respective emails.

Now we have learn Send Forgotten Password and Username in Email in ASP.Net C# with example

Step 1 : Create database

CREATE TABLE [dbo].[user_master] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[name] NVARCHAR (50) NOT NULL,
[password] NVARCHAR (50) NOT NULL,
[email] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);

Step 2: Create Data access class and give name DAL.cs

public class DAL
{
public int id { get; set; }
public string name { get; set; }
public string password { get; set; }
public string email { get; set; }
}

Step 3 : Create Business Access Layer and give name BAL.cs

public class BAL
{
SqlConnection con = new SqlConnection(@”Data Source=(LocalDB)Db1.mdf;Integrated Security=True“);public DataTable Forgotpwd(DAL da)
{
con.Open();
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(“Select name,password from user_master where
email= ‘” + da.email + “‘”, con);adp.Fill(dt);
con.Close();
return dt;
}
}

Step 4 : When the user enters his email address and the Button is clicked the following event handler is executed. Inside this event handler, a SQL Query is executed on the Login table and the user’s record is fetched using the supplied email address

Send Forgotten Password and Username in Email in ASP.Net C#

In WebForm

public partial class forgotpassword : System.Web.UI.Page
{
BAL ba = new BAL();
DAL da = new DAL();
DataTable dt = new DataTable();
DataSet ds = new DataSet();

protected void Button1_Click(object sender, EventArgs e)
{

da.email =txt_mail.Text;
dt = ba.Forgotpwd(da);
if (dt.Rows.Count > 0)
{
MailMessage email = new MailMessage();
email.From = new MailAddress(txt_mail.Text);
email.To.Add(txt_mail.Text);
email.Subject = “Your Forgot Password:“;
email.Body = “Hi,Your Username is: ” + ds.Tables[0].Rows[0][“name”] + ”
Your Password is: ” + ds.Tables[0].Rows[0][“password”] + ” “;
email.IsBodyHtml = true;
SmtpClient smtpc = new SmtpClient();
smtpc.Port = 587;
smtpc.UseDefaultCredentials = false;
smtpc.EnableSsl = true;
Label1.Text = “xyz123@gmail.com“;
Label2.Text = “1234567890“;
smtpc.Credentials = new NetworkCredential(Label1.Text, Label2.Text);
smtpc.Send(email);
Label1.Text = “Your password has been sent to your email address“;

}
else
{
Label2.Text = “This email address is not exist in our Database try again“;
}

SEE MORE

5 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *