User:Sureshbabuvnmn/sandbox

http://www.aspdotnet-suresh.com/2012/11/code-for-forgot-password-in-aspnet.html

Code for Forgot Password in Asp.net using C#, VB.NET with Email By: Suresh Dasari Nov 1, 2012 Categories: Asp.net, C#.Net , SendMail , VB.NET Introduction:

In this article I will explain how to write code to recover forgot password in asp.net using c# and vb.net.

Description:

In previous posts I explained Create ContactUs Page in asp.net, send mail using gmail account in asp.net,send mail with images using gmail in asp.net, send html page as mail body in asp.net and many articles onsend mail in asp.net. Now I will explain how to write code to recover forgot password in asp.net using c#and vb.net.

Before implement this first design one table UserInfo in your database like as shown below

Column Name	Data Type	Allow Nulls UserId	int (Set Identity=true)	No UserName	varchar(50)	Yes Email	varchar(125)	Yes Location	varchar(50)	Yes Once table design complete enter some dummy for our example like as shown below

UserInfo

Now we are going to use our gmail account credentials to send mail for that first you need to enable POP enable option in your Gmail account for that you need to open your gmail account and go to Settings-->Forwarding and POP/IMAP

After that design your aspx page like this

  Code to recover forgot password in asp.net using C# and VB.NET  .Button { background-color :#FF5A00; color: #FFFFFF; font-weight: bold; margin-right: 2px; padding: 4px 20px 4px 21px; }  After that add following namespaces in your codebehind

C# Code

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

After that write the following code in button click

protected void btnSubmit_Click(object sender, EventArgs e) { try { DataSet ds = new DataSet; using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")) { con.Open; SqlCommand cmd = new SqlCommand("SELECT UserName,Password FROM UserInfo 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, Please check your Login Detailss Your Username: " + ds.Tables[0].Rows[0]["UserName"] + "  Your Password: " + ds.Tables[0].Rows[0]["Password"] +"  "; 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 ("yourusername@gmail.com", "yourpassword"); 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); } } } VB.NET Code

Imports System.Data Imports System.Data.SqlClient Imports System.Net.Mail Partial Class VBCode Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) End Sub

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Try Dim ds As New DataSet Using con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB") con.Open Dim cmd As New SqlCommand("SELECT UserName,Password FROM UserInfo Where Email= '" & txtEmail.Text.Trim & "'", con) Dim da As New SqlDataAdapter(cmd) da.Fill(ds) con.Close End Using If ds.Tables(0).Rows.Count > 0 Then Dim Msg As 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, Please check your Login Detailss Your Username: " & Convert.ToString(ds.Tables(0).Rows(0)("UserName")) & "  Your Password: " & Convert.ToString(ds.Tables(0).Rows(0)("Password")) & "  " Msg.IsBodyHtml = True ' your remote SMTP server IP. Dim smtp As New SmtpClient smtp.Host = "smtp.gmail.com" smtp.Port = 587 smtp.Credentials = New System.Net.NetworkCredential ("yourusername@gmail.com", "yourpassword") 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." End If Catch ex As Exception Console.WriteLine("{0} Exception caught.", ex) End Try End Sub End Class Demo Download Sample code Attached

Go to sql server create one db and create table in that  with password

Me successs:

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

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=VSB;User id=sa;pwd=123;Integrated Security=true;Initial Catalog=LOGIN")) {               con.Open; SqlCommand cmd = new SqlCommand("SELECT UserName,Password FROM SAP 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, Please check your Login Detailss Your Username: " + ds.Tables[0].Rows[0]["UserName"] + "  Your Password: " + ds.Tables[0].Rows[0]["Password"] + "  "; 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("sureshbabuvnmn@gmail.com", "tendulkar415"); 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); }   } }