Thursday, 23 December 2021

Forgot Password Recovery and Send Email Using ASP.Net Using C#

12/23/2021 - By Pranav Singh 0

Forgot Password Recovery and Send Email Using ASP.Net Using C#

In this you will learn how you can perform forgot password or implement forgot password or password recovery and send password in user provided email in asp.net using c#.net.

So for this article first we will create a new asp.net application and add the below form code into the page.

Now we will generate the check event and add the below code into the page. So first we will write code to send the email. Please check the below code for sending mail of user password.

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............";
            }
        }

In above code I have used gmail as smtp to send the password of user in mail. Now we will check the code to retrieve the user password form database.

  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;
        }

In above code I have check the table for user password by his email id. If email id is there then retrieve the password and return it for further processing.

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.";
            }
        }

In above code first I have checked weather the provided email id is valid or not after that I have retrieved the detail and pass it for further processing.

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............";
            }
        }
    }

}

Now we have done run the application to check the output. On providing wrong email.

On providing the correct email id.

Now check the email


Tags: ,
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top