private AlternateView Mail_Body(string username, string password)
{
string str = @"
<!DOCTYPE html>
<html>
<body>
<p>
<h4>This is your Login Details </h4>
</p>
<p>Username: " + username + @"</p>
<p>Password: " + password + @"</p>
<p>
Note:We advise change of password after logging in.
</p>
<p>
Please do not hesitate to contact us if you need any assistance
</p>
<p>
</p>
</body>
</html>
";
AlternateView AV =
AlternateView.CreateAlternateViewFromString(str, null, MediaTypeNames.Text.Html);
return AV;
}
protected void SendEmail(object sender, EventArgs e)
{
string username = string.Empty;
string password = string.Empty;
string constr = ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Firstname, [Password] FROM registration WHERE Email = @Email"))
{
cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.HasRows)
{
while (sdr.Read())
{
username = sdr["Firstname"].ToString();
password = sdr["Password"].ToString();
}
}
}
con.Close();
}
}
if (!string.IsNullOrEmpty(password))
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("sender@gmail.com");
mail.To.Add("User@gmail.com");
mail.Subject = "Password Recovery";
mail.IsBodyHtml = true;
mail.AlternateViews.Add(Mail_Body("username", "Password"));
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("email Username", "email Password");
SmtpServer.EnableSsl = false;
SmtpServer.Send(mail);
}
}