Hi Phonghue,
kindly refer below Sample.
HTML
Email Address:
<asp:TextBox ID="txtEmail" runat="server" Width="250" />
<br />
<asp:Label ID="lblMessage" runat="server" />
<br />
<asp:Button Text="Forgot Username" runat="server" OnClick="SendEmail" />
Namespaces
C#
using System.Net;
using System.Net.Mail;
using System.Drawing;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Net
Imports System.Net.Mail
Imports System.Drawing
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void SendEmail(object sender, EventArgs e)
{
string username = string.Empty;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Username FROM Users WHERE Email = @Email"))
{
cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.Read())
{
username = sdr["Username"].ToString();
}
}
con.Close();
}
}
if (!string.IsNullOrEmpty(username))
{
MailMessage mm = new MailMessage("sender@gmail.com", txtEmail.Text.Trim());
mm.Subject = "Username Recovery";
mm.Body = string.Format("Your Username is {0}.<br /><br />Thank You.", username);
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "sender@gmail.com";
NetworkCred.Password = "<Password>";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
lblMessage.ForeColor = Color.Green;
lblMessage.Text = "Username has been sent to your email address.";
}
else
{
lblMessage.ForeColor = Color.Red;
lblMessage.Text = "This email address does not match our records.";
}
}
VB.Net
Protected Sub SendEmail(ByVal sender As Object, ByVal e As EventArgs)
Dim username As String = String.Empty
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT Username FROM Users WHERE Email = @Email")
cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim())
cmd.Connection = con
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
If sdr.Read() Then
username = sdr("Username").ToString()
End If
End Using
con.Close()
End Using
End Using
If Not String.IsNullOrEmpty(username) Then
Dim mm As MailMessage = New MailMessage("sender@gmail.com", txtEmail.Text.Trim())
mm.Subject = "Username Recovery"
mm.Body = String.Format("Your Username is {0}.<br /><br />Thank You.", username)
mm.IsBodyHtml = True
Dim smtp As SmtpClient = New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
Dim NetworkCred As NetworkCredential = New NetworkCredential()
NetworkCred.UserName = "sender@gmail.com"
NetworkCred.Password = "<Password>"
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(mm)
lblMessage.ForeColor = Color.Green
lblMessage.Text = "Username has been sent to your email address."
Else
lblMessage.ForeColor = Color.Red
lblMessage.Text = "This email address does not match our records."
End If
End Sub