Here is the another approach which will also work:
public string getUserPassword(string Email)
{
string body = "";
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=blaPC\\SQLEXPRESS; Initial Catalog=forgetpassword; Integrated Security=True";
con.Open();
string procedureText = "retriveUserPassword";
SqlCommand cmd = new SqlCommand(procedureText, con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@Email", Email));
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
body = "Your Password is " + dr[0].ToString();
}
else
{
HttpContext.Current.Response.Write("<script>alert('Email does not exist')</script>");
}
dr.Dispose();
dr = null;
return body;
}
protected void Button1_Click(object sender, EventArgs e)
{
myclass obj = new myclass();
try
{
MailMessage mail = new MailMessage();
string body = obj.getUserPassword(TextBox1.Text);
if (body.Length > 0)
{
mail.To.Add(TextBox1.Text);
mail.From = new MailAddress("farooqspecials@gmail.com");
mail.Subject = "My Project Password Recovery System";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential
("farooqspecials@gmail.com", "blablabla");
smtp.EnableSsl = true;
smtp.Send(mail);
Label1.Text = "Dear User Your Password is Sent to your Email Address";
}
}
catch (Exception ex)
{
Label1.Text = "some Problem" + ex;
}
finally
{
myclass = null;
}
}