Thursday 3 November 2016

// // Leave a Comment

How to send forget password on registered email id in asp.net c#

Sent a mail to the user‘s email id with his forgotten password .we have a database where we fetch the email id and password and send a email with the password.

For more:send forgot password link on email for reset in asp.net C# ,Login page in asp.net c# with sql database , Pop up login page in asp.net c# using CSS and java script.


SQL server:-

Script:-
CREATE DATEBASE[CodeSolution]

USE [CodeSolution]
GO

/*************/

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[record](
          [record_id] [int] IDENTITY(1,1) NOT NULL,
          [Name] [nvarchar](50) NULL,
          [Contact_no] [nvarchar](50) NULL,
          [Email] [nvarchar](50) NULL,
          [Password] [nvarchar](50) NULL,
 CONSTRAINT [PK_record] PRIMARY KEY CLUSTERED
(
          [record_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  =OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON,ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

Source Code:-

<%@ Page Language="C#" AutoEventWireup="true"CodeFile="Forgetpassword.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">
    <fieldset style="width: 350px;" align="center">
        <legend>Send Forget Password In Registerd Email </legend>
        <asp:TextBox ID="txtemail" runat="server" Width="199px"Placeholder="Enter Your Registed Email ID"></asp:TextBox><br />
        <br />
        <asp:Button ID="Button1" runat="server"OnClick="Button1_Click" Text="Submit" Width="78px" />
        <br />
        <asp:Label ID="lbresult" runat="server" Text=""></asp:Label>
    </fieldset>
    </form>
</body>
</html>


Code behind(C#):-

using System;
using System.Data.SqlClient;
using System.Data;
using System.Net.Mail;


using System.Net;
using System.Drawing;

public partial class _Default : System.Web.UI.Page

{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=NEERAJ-PC;Initial Catalog=CodeSolution;Persist Security Info=True;User ID=sa; password=12345678");
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT Email,Password FROM record Where Email= '" + txtemail.Text + "'", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.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 From Code Solutions";//Subject for your request
            email.Body += "Hi,<br/>Your Email ID is: " + ds.Tables[0].Rows[0]["Email"] + "<br/><br/>Your Password is: " + ds.Tables[0].Rows[0]["Password"] + "<br/> <br/> Thanks <br/> Code Solutions";
            email.IsBodyHtml = true;
            //SMTP SERVER DETAILS
            SmtpClient smtpc = new SmtpClient("smtp.gmail.com");
            smtpc.Port = 587;
            smtpc.UseDefaultCredentials = false;
            smtpc.EnableSsl = true;
            smtpc.Credentials = newNetworkCredential("hmclko@gmail.com""9450216522");
            smtpc.Send(email);
            lbresult.Text = "Your Password Has Been Sent To Your Email ID";
            lbresult.ForeColor = Color.Green;
           
        }
        else
        {

            lbresult.Text = "Please Enter Correct Email ID";
            lbresult.ForeColor = Color.Red;

        }


        txtemail.Text = "";
    }

}

0 comments:

Post a Comment