Share Blog

Monday, May 19, 2014

How to Send Forgot Password in Email or Example of Forgot Password in Asp.Net

Description:-

                        In this Example we Explain that How to Send Password to user Email when user click on Forgot password and enter his/her Email.

Here we can not send other password or autogenerate password. We simply send current password of the user to his/her email.
This Example is very useful when user Forgot his password and we can easily give his password by sendin in Email.

We all know that in Corporate world today this Forgot password Facility available in Every place.so you can easily send Forgot password to the user Email or user mobile number by sending SMS to user in your mobile.

First to do this You have to create a one table like

Default.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <table cellspacing="2" cellpadding="2" border="0">
<tr><td></td><td><b>Forgot Password Example</b></td></tr>
<tr><td><b>Enter Your Email:</b></td><td><asp:TextBox ID="txtEmail" runat="server" /></td></tr>
<tr><td></td><td><asp:button ID="btnSubmit" Text="Submit"  runat="server" onclick="btnSubmit_Click" CssClass="Button"/></td></tr>
<tr><td colspan="2" style=" color:red"><asp:Label ID="lbltxt" runat="server"/></td></tr>
</table>
    </div>
    </form>
</body>
</html>

Default.aspx.cs:-
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
     {
       DataSet ds = new DataSet();
       using (SqlConnection con = new SqlConnection("Data Source=SUNIL;Initial Catalog=adonetpractices;Integrated Security=True"))
       {
         con.Open();
        SqlCommand cmd = new SqlCommand("SELECT username,password FROM Login Where email= '" + txtEmail.Text.Trim() + "'", con);
      SqlDataAdapter da = new SqlDataAdapter(cmd);
     da.Fill(ds);
      con.Close();
        }
if(ds.Tables[0].Rows.Count>0)
{
  

MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtEmail.Text);
// Recipient e-mail address.
Msg.To.Add(txtEmail.Text);
Msg.Subject = "Your Password Details";
Msg.Body = "Hi, <br/>Please check your Login Detailss<br/><br/>Your Username: " + ds.Tables[0].Rows[0]["username"] + "<br/><br/>Your Password: " + ds.Tables[0].Rows[0]["password"] + "<br/><br/>";
Msg.IsBodyHtml = true;
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential ("Your Email Id", "Your Password");
smtp.EnableSsl = true;
smtp.Send(Msg);
//Msg = null;
lbltxt.Text = "Your Password Details Sent to your mail";
// Clear the textbox valuess
txtEmail.Text = "";
}
else
{
lbltxt.Text = "The Email you entered not exists.";
}
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
    }



No comments:

Post a Comment