This
article will show you how you can perform forgot password or implement forgot
password or password recovery and send password in user provided email in
asp.net using c#.net.
Some of my
previous articles are as follows: Simple
Login From in Asp.Net MVC Using C#.Net, Simple
Login Form In Asp.Net Using C#.Net and VB.Net | How to Create Login Form in
Asp.Net Using C#, Contact
Us Form With Browse File Attachment in Asp.net C#, Contact
Us Form in MVC5 & MVC6 C#, Contact
Us Form in Asp.net C#, How
to Send Mail in MVC6 Using C# Code With Attachment, Send
Mail In MVC5 and MVC6 In C# Using Gmail, Send
Mail In Asp.Net C# Using Gmail, The
SMTP server requires a secure connection or the client was not authenticated.
The server response was: 5.5.1 Authentication Required. Learn more at.
So for this article first we will create a new asp.net application and add the below form code into the page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Forgotpassword.aspx.cs" Inherits="WebApplication7.Forgotpassword" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Implement Forgot
Password (Password Recovery) and send password in Email in ASP.Net
</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" Style="color: #FF0000" />
Forgot
Password:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:RequiredFieldValidator
ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="Pleasse enter email id">
*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="Please
enter a valid email id" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click"
/>
<br />
<asp:Label ID="Label1" runat="server" style="color: #FF0000"></asp:Label>
</div>
</form>
</body>
</html>
|
In above
code I have created a form for forgot password. This have validated by using RequiredFieldValidator
and RegularExpressionValidator.
Now we will
generate the check event and add the below code into the page. So first we will write code to send the
email. Please check the below code for sending mail of user password.
public string SendPassword(string password, string emailId, string name)
{
try
{
MailMessage mail
= new MailMessage();
mail.To.Add(emailId);
mail.From = new MailAddress("youremail
@gmail.com");
mail.Subject = "Your password for
account " + emailId;
string userMessage = "";
userMessage = userMessage + "<br/><b>Login
Id:</b> " + emailId;
userMessage = userMessage + "<br/><b>Passsword:
</b>" + password;
string Body = "Dear " +
name + ", <br/><br/>Login
detail for your account is a follows:<br/></br> " + userMessage + "<br/><br/>Thanks";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp
= new SmtpClient();
smtp.Host = "smtp.gmail.com"; //SMTP Server Address of
gmail
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("youremail@gmail.com", "password");
// Smtp Email ID and Password For authentication
smtp.EnableSsl = true;
smtp.Send(mail);
return "Please check your email for account login
detail.";
}
catch
{
return "Error............";
}
}
|
In above
code I have used gmail as smtp to send the password of user in mail. Now we
will check the code to retrieve the user password form database.
public DataTable GetData(string emailId)
{
DataTable dt = new DataTable();
SqlConnection con =
new SqlConnection("Your connection string");
SqlDataAdapter da = new SqlDataAdapter("Select * from Table where EmailId='" + emailId + "';", con);
con.Open();
da.Fill(dt);
con.Close();
return dt;
}
|
In above
code I have check the table for user password by his email id. If email id is
there then retrieve the password and return it for further processing.
Now we will
check he click event of the button to manage the forgot password.
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt =
GetData(TextBox1.Text);
if (dt.Rows.Count > 0)
{
//Send login detail to user email
string password =
dt.Rows[0]["Name"].ToString();
string emailId = dt.Rows[0]["EmailId"].ToString();
string name =
dt.Rows[0]["Password"].ToString();
string status =
SendPassword(password, emailId, name);
Label1.Text = status;
}
else
{
//if
email id is not register with us
Label1.Text = "Please provide a
valid register email id.";
}
}
|
In above
code first I have checked weather the provided email id is valid or not after
that I have retrieved the detail and pass it for further processing.
Now check
the complete code.
using System;
using
System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data.SqlClient;
namespace WebApplication7
{
public partial class Forgotpassword :
System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt =
GetData(TextBox1.Text);
if (dt.Rows.Count > 0)
{
//Send login detail to user email
string password =
dt.Rows[0]["Name"].ToString();
string emailId =
dt.Rows[0]["EmailId"].ToString();
string name =
dt.Rows[0]["Password"].ToString();
string status =
SendPassword(password, emailId, name);
Label1.Text = status;
}
else
{
//if email id is not register with us
Label1.Text = "Please provide a
valid register email id.";
}
}
public DataTable GetData(string emailId)
{
DataTable dt = new DataTable();
SqlConnection con =
new SqlConnection("Your connection string");
SqlDataAdapter da = new SqlDataAdapter("Select * from Table
where EmailId='" + emailId + "';",
con);
con.Open();
da.Fill(dt);
con.Close();
return dt;
}
public string SendPassword(string password, string emailId, string name)
{
try
{
MailMessage mail
= new MailMessage();
mail.To.Add(emailId);
mail.From = new MailAddress("youremail@gmail.com");
mail.Subject = "Your password for
account " + emailId;
string userMessage = "";
userMessage = userMessage + "<br/><b>Login
Id:</b> " + emailId;
userMessage = userMessage + "<br/><b>Passsword:
</b>" + password;
string Body = "Dear " +
name + ", <br/><br/>Login
detail for your account is a follows:<br/></br> " + userMessage + "<br/><br/>Thanks";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp
= new SmtpClient();
smtp.Host = "smtp.gmail.com"; //SMTP Server Address of
gmail
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("youremail@gmail.com", "Password");
// Smtp Email ID and Password For authentication
smtp.EnableSsl = true;
smtp.Send(mail);
return "Please check your email for account login
detail.";
}
catch
{
return "Error............";
}
}
}
}
|
Now we have
done run the application to check the output. On providing wrong email.
On providing
the correct email id.
Now check
the email
0 comments:
Please let me know your view