How to Send Forget Password using Asp.Net

How to Send Forget Password using Asp.Net.
                    

Hello Friends,We are here to share how to send forget password on your registered mail using asp.Net.Here we have used  SMTP server for sending the password to the registered email address. SMTP server is fully secure that's why we have send password to registered mail through SMTP server.You can able to download this file and make changes in that file as you like.You can see download link at the bottom of this post.

Source code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Forgetpwd.aspx.cs" Inherits="Forgetpassword.Forgetpwd" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>NV2</title>
    <style>
        html, body{height:100%;}

.forgetimg
{
background: url(Image/forgot-password.gif);
background-size: cover;
background-repeat: no-repeat;
height:350px;
width:450px;
}
.btnsubmit
{
    padding: 10px 15px;
    background: #4479BA;
    color: #FFF;
}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div><h2 style="text-align:center">Forget Password</h2>
        <table align="center" class="forgetimg">
            <tr>
       <td><asp:Label ID="LblEmail" runat="server" Text="Enter Your Email Id"></asp:Label></td>
       <td> <asp:TextBox ID="TxtEmail" runat="server"></asp:TextBox></td></tr>
      <tr> <td colspan="2" style="text-align:center"><asp:Button ID="BtnSubmit" runat="server" CssClass="btnsubmit" Text="Submit" OnClick="BtnSubmit_Click" /></td>
        </tr>
        </table>
       <div style="text-align:center"><asp:Label ID="LblSenderEmail" runat="server" Text="Label" Visible="false"></asp:Label>
        <asp:Label ID="LblPassword" runat="server" Text="Label" Visible="false"></asp:Label>
        <asp:Label ID="LblError" runat="server" Text="Label" Visible="false"></asp:Label></div>
    </div>
    </form>
</body>

</html>


C# Code:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


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

        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {

            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                SqlCommand cmd = new SqlCommand("SELECT username,password FROM Fpwd_tab Where email= '" + TxtEmail.Text + "'", con);
                {
                    con.Open();
                    cmd.Parameters.AddWithValue("@email", TxtEmail.Text);
                    SqlDataAdapter ad = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    ad.Fill(ds);
                    con.Close();


                    if (ds.Tables[0].Rows.Count > 0)
                    {
                       
                        MailMessage email = new MailMessage();
                        email.From = new MailAddress(TxtEmail.Text); //Enter sender email address
                        email.To.Add(TxtEmail.Text); //Destination Recipient e-mail address.
                        email.Subject = "Your Forget Password:";//Subject for your request
                        email.Body = "Hi,Your Username is: " + ds.Tables[0].Rows[0]["username"] + "<br/>" +  "Your Password is: " + ds.Tables[0].Rows[0]["Password"] + "";
                        email.IsBodyHtml = true;
                        //SMTP SERVER DETAILS
                        SmtpClient smtpc = new SmtpClient("smtp.gmail.com");
                        smtpc.Port = 587;
                        smtpc.UseDefaultCredentials = false;
                        smtpc.EnableSsl = true;
                        LblSenderEmail.Text = "abc@gmail.com"; //<--Enter your gmail id here
                        LblPassword.Text = "123";//<--Enter gmail password here
                        smtpc.Credentials = new NetworkCredential(LblSenderEmail.Text, LblPassword.Text);
                        smtpc.Send(email);
                        //LblSenderEmail.Visible = true;
                        LblPassword.Visible = true;
                        LblPassword.Text = "Your password has been sent to your email address";
                    }
                    else
                    {

                        LblError.Visible = true;
                        LblError.Text = "This email address is not exist in our Database try again";
                    }
                }

            }
        }
    }

}

Web.config:

<configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>
 <connectionStrings>
    <add name="constr" connectionString="Data Source=(local);Initial Catalog=Forgetpwd;User ID=abc;Password=abc@123"
         providerName="System.Data.SqlClient" />

  </connectionStrings>
  <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  </appSettings>
</configuration>



Output:
Fig: Sending forget password to gmail.

Download



Thanks for visiting,

We hope this may help you.

Share this

Related Posts

Latest
Previous
Next Post »