This Example explains how to Create Forgot Password By Email Page Code In Asp.Net Using C# And VB.NET.
I have placed one textbox and button on the ForgotPassword.aspx page to send mail to email id stored in database.
You can also send Reset Password Link instead of sending Username password in the email.
HTML SOURCE OF FORGOT PASSWORD PAGE
Write following code in Click Event of Button to retrieve username and password associated with EmailID provided by user from database and send the information to this email id.
C# CODE
VB.NET
I have placed one textbox and button on the ForgotPassword.aspx page to send mail to email id stored in database.
You can also send Reset Password Link instead of sending Username password in the email.
HTML SOURCE OF FORGOT PASSWORD PAGE
1: <form id="Form1" runat="server">
2: <div>
3: <fieldset>
4: <legend>Forgot Password</legend>
5: <asp:Label ID="lblEmail" runat="server" Text="Email Address: "/>
6: <asp:TextBox ID="txtEmail" runat="server"/>
7:
8: <asp:RequiredFieldValidator ID="RV1" runat="server"
9: ControlToValidate="txtEmail"
10: ErrorMessage="Please Enter EmailID"
11: SetFocusOnError="True">*
12: </asp:RequiredFieldValidator>
13:
14: <asp:Button ID="btnPass" runat="server" Text="Submit"
15: onclick="btnPass_Click"/>
16:
17: <asp:ValidationSummary ID="ValidationSummary1"
18: runat="server" CssClass="error"/>
19:
20: <asp:Label ID="lblMessage" runat="server" Text=""/>
21: </fieldset>
22: </div>
23: </form>
Write following code in Click Event of Button to retrieve username and password associated with EmailID provided by user from database and send the information to this email id.
C# CODE
01
using
System;
02
using
System.Data.SqlClient;
03
using
System.Configuration;
04
using
System.Data;
05
using
System.Net.Mail;
06
07
protected
void
btnPass_Click(
object
sender, EventArgs e)
08
{
09
//Create Connection String And SQL Statement
10
string
strConnection = ConfigurationManager.ConnectionStrings[
"ConnectionString"
].ConnectionString;
11
string
strSelect =
"SELECT UserName,Password FROM Users WHERE Email = @Email"
;
12
13
SqlConnection connection =
new
SqlConnection(strConnection);
14
SqlCommand command =
new
SqlCommand();
15
command.Connection = connection;
16
command.CommandType = CommandType.Text;
17
command.CommandText = strSelect;
18
19
SqlParameter email =
new
SqlParameter(
"@Email"
, SqlDbType.VarChar, 50);
20
email.Value = txtEmail.Text.Trim().ToString();
21
command.Parameters.Add(email);
22
23
//Create Dataset to store results and DataAdapter to fill Dataset
24
DataSet dsPwd =
new
DataSet();
25
SqlDataAdapter dAdapter =
new
SqlDataAdapter(command);
26
connection.Open();
27
dAdapter.Fill(dsPwd);
28
connection.Close();
29
if
(dsPwd.Tables[0].Rows.Count > 0 )
30
{
31
MailMessage loginInfo =
new
MailMessage();
32
loginInfo.To.Add(txtEmail.Text.ToString());
33
loginInfo.From =
new
MailAddress(
"YourID@gmail.com"
);
34
loginInfo.Subject =
"Forgot Password Information"
;
35
36
loginInfo.Body =
"Username: "
+ dsPwd.Tables[0].Rows[0][
"UserName"
] +
"<br><br>Password: "
+ dsPwd.Tables[0].Rows[0][
"Password"
] +
"<br><br>"
;
37
loginInfo.IsBodyHtml =
true
;
38
SmtpClient smtp =
new
SmtpClient();
39
smtp.Host =
"smtp.gmail.com"
;
40
smtp.Port = 587;
41
smtp.EnableSsl =
true
;
42
smtp.Credentials =
new
System.Net.NetworkCredential(
"YourGmailID@gmail.com"
,
"YourGmailPassword"
);
43
smtp.Send(loginInfo);
44
lblMessage.Text =
"Password is sent to you email id,you can now <a href="
Login.aspx
">Login</a>"
;
45
}
46
else
47
{
48
lblMessage.Text =
"Email Address Not Registered"
;
49
}
50
51
}
VB.NET
01
Imports
System.Data.SqlClient
02
Imports
System.Configuration
03
Imports
System.Data
04
Imports
System.Net.Mail
05
06
Protected
Sub
btnPass_Click(sender
As
Object
, e
As
EventArgs)
07
'Create Connection String And SQL Statement
08
Dim
strConnection
As
String
= ConfigurationManager.ConnectionStrings(
"ConnectionString"
).ConnectionString
09
Dim
strSelect
As
String
=
"SELECT UserName,Password FROM Users WHERE Email = @Email"
10
11
Dim
connection
As
New
SqlConnection(strConnection)
12
Dim
command
As
New
SqlCommand()
13
command.Connection = connection
14
command.CommandType = CommandType.Text
15
command.CommandText = strSelect
16
17
Dim
email
As
New
SqlParameter(
"@Email"
, SqlDbType.VarChar, 50)
18
email.Value = txtEmail.Text.Trim().ToString()
19
command.Parameters.Add(email)
20
21
'Create Dataset to store results and DataAdapter to fill Dataset
22
Dim
dsPwd
As
New
DataSet()
23
Dim
dAdapter
As
New
SqlDataAdapter(command)
24
connection.Open()
25
dAdapter.Fill(dsPwd)
26
connection.Close()
27
If
dsPwd.Tables(0).Rows.Count > 0
Then
28
Dim
loginInfo
As
New
MailMessage()
29
loginInfo.[
To
].Add(txtEmail.Text.ToString())
30
loginInfo.From =
New
MailAddress(
"YourID@gmail.com"
)
31
loginInfo.Subject =
"Forgot Password Information"
32
33
loginInfo.Body =
"Username: "
& Convert.ToString(dsPwd.Tables(0).Rows(0)(
"UserName"
)) &
"<br><br>Password: "
& Convert.ToString(dsPwd.Tables(0).Rows(0)(
"Password"
)) &
"<br><br>"
34
loginInfo.IsBodyHtml =
True
35
Dim
smtp
As
New
SmtpClient()
36
smtp.Host =
"smtp.gmail.com"
37
smtp.Port = 587
38
smtp.EnableSsl =
True
39
smtp.Credentials =
New
System.Net.NetworkCredential(
"YourGmailID@gmail.com"
,
"YourGmailPassword"
)
40
smtp.Send(loginInfo)
41
lblMessage.Text =
"Password is sent to you email id,you can now <a href="
Login.aspx
">Login</a>"
42
Else
43
lblMessage.Text =
"Email Address Not Registered"
44
End
If
45
46
End
Sub
If you like this post than join us or share
8 comments:
The code is valid if the password is stored in clear text format. Most of the times it is Hashed through one way hash algorithms like SHA1. In that situation, all you can do is to send a mail with a link which has a another hash value and you ask the user to change the password on that page. That page is only usable once.
Anyway above code is a good and simple example :)
How and which SQL database has to be connected
line number 40: "..Login.aspx">Login" it says Only assignment, call, increment, decrement, and new object expressions can be used as a statement.
@Pankaj: Hi, Thanks for writing, you are absolutely correct, i wrote this article for beginners to get an idea how forgot password works, i did mention, if you had not overlooked, that we can send reset password link as well, keep visiting
@Deovrat A Sharp Spark : Please use single quote ' insted of double quotes in href="Login.aspx"
lblMessage.Text = "Password is sent to you email id,you can now <a href='Login.aspx'>Login</a>"
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
i am using authentication tag for login form but the after login page is getting open via address. if i put address in address bar
Error:
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
Post a Comment