connectseekers.com

Thursday, 8 September 2016

Implement Forgot Password (Password Recovery) and send password in Email in ASP.Net Using C#

9/08/2016 - By Pranav Singh 0

This article will show you 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.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Forgotpassword.aspx.cs" Inherits="WebApplication7.Forgotpassword" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Implement Forgot Password (Password Recovery) and send password in Email in ASP.Net
</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ValidationSummary ID="ValidationSummary1" runat="server" Style="color: #FF0000" />
            Forgot Password:
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="Pleasse enter email id">
                *</asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
                ErrorMessage="Please enter a valid email id" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
            <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
            <br />
            <asp:Label ID="Label1" runat="server" style="color: #FF0000"></asp:Label>
        </div>
    </form>
</body>
</html>

In above code I have created a form for forgot password. This have validated by using RequiredFieldValidator and RegularExpressionValidator.

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.

  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




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
Email Id : contact@aspdotnet-pools.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


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top