How to Recover Forgot Password via Send Email in Asp.net | ASPMANTRA | Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server AND WCF snippets and tutorial

How to Recover Forgot Password via Send Email in Asp.net

Introduction: In this post I will explain how we can recover the lost/forgot password via email in asp.net.
Forgot Password

Forgot Password


Description:
In the last post I have explained How to Create Login Form in Asp.net OR Login Form with Check Username and Password aviabilty in Asp.net, How to create registration page in asp.net.

Users click on forgot password link and redirect to forgot password page. There user enter their Email Id and get their password details via Email.

To explain forgot Password Example I created a Table USER_REGISTRATION:
ID
int
USERNAME
varchar(50)
PASSWORD
varchar(50)
FIRST_NAME
varchar(50)
LAST_NAME
varchar(50)
SEX
varchar(50)
EMAIL_ID
varchar(50)

Here ID is autoincrement and primary key and have the record show in sanpshot:

Recover Forgot Password

Add a webform to project. Drag and drop the controls from Toolbox and design the .aspx page as shown below:
<table align="center">
    <tr><td></td></tr>
     <tr><td><h1>Forgot Password Example:</h1></td></tr>
      <tr><td>Enter Your Email:</td><td>
          <asp:TextBox ID="txtemail" runat="server" Width="220px" Height="30px"></asp:TextBox></td></tr>
       <tr><td></td></tr>
        <tr><td>&nbsp;</td><td>
            <asp:Button ID="txtsubmit" runat="server" Text="Send" OnClientClick="return ValidateEmail();"
                onclick="txtsubmit_Click" /></td></tr>
         <tr><td></td></tr>
    </table>

Put the below given script between Head Tag to validate the Email ID:
<script language="javascript" type="text/javascript">
       function ValidateEmail() {
           var emailRegex = new RegExp(/^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$/i);
           var emailAddress = document.getElementById("<%= txtemail.ClientID %>").value;
           var valid = emailRegex.test(emailAddress);
           if (!valid) {
               alert("Please Enter Valid Email address");
               return false;
           } else
               return true;
       }
  </script>

NOTE: Do not forget to add ConnectionString in web.config file of projec/website:
<connectionStrings>
      <add name="Connection" connectionString="Data Source=VIJAY-PC;Initial Catalog=TEST_APPLICATION;Integrated Security=True"/>
    </connectionStrings>

Now on button click write the below given code (.aspx.cs):
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Configuration;
using System.Text;

protected void txtsubmit_Click(object sender, EventArgs e)
    {       
        try
        {
            DataSet ds = new DataSet();
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString());
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select USERNAME, PASSWORD from USER_REGISTRATION where EMAIL_ID ='" + txtemail.Text.Trim() + "'", con);
                SqlDataAdapter adp = new SqlDataAdapter(cmd);               
                adp.Fill(ds);
                con.Close();
            }
            if (ds.Tables[0].Rows.Count > 0)
            {
                string USERNAME = ds.Tables[0].Rows[0]["USERNAME"].ToString();
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress("saklanivijay87@gmail.com", "Vijay Saklani");
                msg.To.Add(txtemail.Text);
                msg.Subject = "Your Password Details";
                StringBuilder mailbody = new StringBuilder();
                mailbody.Append("Hi &nbsp;&nbsp;" + USERNAME + ",<br/>");
                mailbody.Append("Please check your Login Details<br/><br/>&nbsp;&nbsp;<b>Your Username:</b> " + ds.Tables[0].Rows[0]["USERNAME"] +
                    "<br/><br/>&nbsp;&nbsp;<b>Your Password: </b>" + ds.Tables[0].Rows[0]["PASSWORD"] + "<br/>");
                mailbody.Append("<br/>");
                mailbody.Append("<a href='http://localhost:10585/NEW_WEBSITE_APPLICATION%2818-07-2013%29/LOGIN_PAGE.aspx'>Click Here to Login Again</a>");
                mailbody.Append("<br/>");
                mailbody.Append("<br/>");
                mailbody.Append("Thanks For Contacting Us");
                mailbody.Append("<br/>");
                mailbody.Append("<br/>");
                mailbody.Append("<a href='#'>Support Team</a>");
                msg.Body = mailbody.ToString();
                msg.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("Your Email", "Password");
                smtp.EnableSsl = true;
                smtp.Send(msg);
                txtemail.Text = "";
                Messagebox("Detail Send Successfully ! Check your Email");
            }
            else
            {
                Messagebox("The Email you entered not exists");
            }
        }
        catch (Exception ex)
        {
           
        }
    }
    private void Messagebox(string Message)
    {
        Label lblMessageBox = new Label();

        lblMessageBox.Text =
            "<script language='javascript'>" + Environment.NewLine +
            "window.alert('" + Message + "')</script>";
        Page.Controls.Add(lblMessageBox);
    }

In VB (.aspx.vb)
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Text
Imports System.Net.Mail

  Protected Sub txtsubmit_Click(sender As Object, e As System.EventArgs) Handles txtsubmit.Click
        Try
            Dim ds As New DataSet()
            Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ConnectionString.ToString())
            If True Then
                con.Open()
                Dim cmd As New SqlCommand("select USERNAME, PASSWORD from USER_REGISTRATION where EMAIL_ID ='" & txtemail.Text.Trim() & "'", con)
                Dim adp As New SqlDataAdapter(cmd)
                adp.Fill(ds)
                con.Close()
            End If
            If ds.Tables(0).Rows.Count > 0 Then
                Dim USERNAME As String = ds.Tables(0).Rows(0)("USERNAME").ToString()
                Dim msg As New MailMessage()
                msg.From = New MailAddress("saklanivijay87@gmail.com", "Vijay Saklani")
                msg.[To].Add(txtemail.Text)
                msg.Subject = "Your Password Details"
                Dim mailbody As New StringBuilder()
                mailbody.Append("Hi &nbsp;&nbsp;" & USERNAME & ",<br/>")
                mailbody.Append(("Please check your Login Details<br/><br/>&nbsp;&nbsp;<b>Your Username:</b> " + ds.Tables(0).Rows(0)("USERNAME") & "<br/><br/>&nbsp;&nbsp;<b>Your Password: </b>") + ds.Tables(0).Rows(0)("PASSWORD") & "<br/>")
                mailbody.Append("<br/>")
                mailbody.Append("<a href='http://localhost:10585/NEW_WEBSITE_APPLICATION%2818-07-2013%29/LOGIN_PAGE.aspx'>Click Here to Login Again</a>")
                mailbody.Append("<br/>")
                mailbody.Append("<br/>")
                mailbody.Append("Thanks For Contacting Us")
                mailbody.Append("<br/>")
                mailbody.Append("<br/>")
                mailbody.Append("<a href='#'>Support Team</a>")
                msg.Body = mailbody.ToString()
                msg.IsBodyHtml = True
                Dim smtp As New SmtpClient()
                smtp.Host = "smtp.gmail.com"
                smtp.Port = 587
                smtp.Credentials = New System.Net.NetworkCredential("Your Email", " Password")
                smtp.EnableSsl = True
                smtp.Send(msg)
                txtemail.Text = ""
                Messagebox("Detail Send Successfully ! Check your Email")
            Else
                Messagebox("The Email you entered not exists")
            End If

        Catch ex As Exception
        End Try
    End Sub
    Private Sub Messagebox(Message As String)
        Dim lblMessageBox As New Label()

        lblMessageBox.Text = "<script language='javascript'>" + Environment.NewLine & "window.alert('" & Message & "')</script>"
        Page.Controls.Add(lblMessageBox)
    End Sub


Run the project and check the result.

Post a Comment

I was curious if you ever considered changing
the layout of your site? Its very well written; I love what
youve got to say. But maybe you could a little more in the way of
content so people could connect with it better. Youve got an awful lot of text for only having one or 2 images.
Maybe you could space it out better?

[blogger]

MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget