Thursday 25 June 2015

Forget Password code using Asp.net with c#


Hi guys,
 This blog we discuss about how to write code for forget password.
 For this when user click  Forget password link button it display forget.aspx page.
 With in this page just enter your email  and then click the send button it will send both password and username to your mail.
Create a table which store the username, email, password . Here table is mypassword.
Forget.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:TextBox ID="txtEmail" runat="server" Height="25px" Width="216px"></asp:TextBox>
        <br />
        <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
        <asp:Label ID="lblMsg" runat="server" Text="Msg"></asp:Label>
   
    </div>
    </form>
</body>
</html>

Forget.aspx.cs page

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

public partial class forgetpassword : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["123"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("select  UserName, PassWord from mypassword where Email ='" + txtEmail.Text + "'", con);
        DataSet ds = new DataSet();
        SqlDataAdapter sdr = new SqlDataAdapter(cmd);
        sdr.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(txtEmail.Text);
            msg.To.Add(txtEmail.Text);
            msg.Subject = "Your password Details";
            msg.Body = "Hi , <br/> plz check your login details<br/>Your UserName: " + ds.Tables[0].Rows[0]["UserName"] + "<br/>Your Password: " + ds.Tables[0].Rows[0]["PassWord"] + "<br/>";
            msg.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential("sanjib03mca@gmail.com", "sanchagulu");
            smtp.EnableSsl = true;
            smtp.Send(msg);
            lblMsg.Text = "Your password Details sent to your mail";
            txtEmail.Text = "";
           
        }
        else
        {
            lblMsg.Text = "Plz enter correct email";
        }
    }
}


No comments:

Post a Comment