C# .NET - Send email to user for password reset

Asked By Muthu Kumar on 26-Jun-12 07:29 AM
if the user forget their password 
how  can i  send reset password link to user mail id .that will use onle one time , after one time use this link will dispose and say that this link is wrong.


Thanks in advance
Lalit M replied to Muthu Kumar on 26-Jun-12 07:35 AM
Use this code for password reset.
-----------------------------
DataSet ds=new DataSet();

ds=fp.viewdatabase("select password from login where
username='"+txtuser.Text+"' and
securityquest='"+dropsecurity.SelectedItem.Text+"' and
answer='"+txtans.Text+"'");
if(ds.Tables[0].Rows.Count>0)
{
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("sender@abc.com");
msg.To.Add(new MailAddress(txtaltmai.Text.Trim()));  // receiver   mail
msg.Subject ="Your correct password given";

msg.Body = "your password is'"+ds.Tables[0].Rows[0]["password"].ToString()
+"'";
msg.IsBodyHtml = true;
SmtpClient sc = new SmtpClient("smtp.gmail.com", 587);
System.Net.NetworkCredential basicAuthentication = new
System.Net.NetworkCredential("sender@abc.com", "developers_choice");
sc.EnableSsl = true;
sc.UseDefaultCredentials = false;
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
sc.Credentials = basicAuthentication;
sc.Send(msg);
Response.Write("Your password has been send to secondary
mailid");

}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}

Read more options.
-----------
http://www.programmerfish.com/send-email-using-gmail-in-aspnet-c/

Using MVC
---------
http://forrst.com/posts/ASP_NET_MVC_3_C_Password_Reset-gFA
dipa ahuja replied to Muthu Kumar on 26-Jun-12 02:28 PM
If you are using custome password recovery the just have to send the mail to the user that your password is:

protected void btnSent_Click(object sender, EventArgs e)
{
  string toEmailAddress = "UserToWhomSendPassword@gmail.com";
  string GmailId = "xxx@gmail.com";
  string password = "xxx";
 
  SqlDataAdapter da = new SqlDataAdapter("Select uname,password from user where username='" + TextBox1.Text
  + "'", "ConnectionSTring");
  DataTable dt = new DataTable();
  da.Fill(dt);
 
 
  string bodyMsg = "Your Username :" + dt.Rows[0]["uname"] + "and passowrd:" + dt.Rows[0]["password"].ToString();
 
 
  MailMessage mail = new MailMessage();
  mail.To.Add(toEmailAddress);
 
  mail.From = new MailAddress(GmailId);
  mail.Subject = txtSubject.Text;
  mail.Body = bodyMsg;
  mail.IsBodyHtml = true;
 
   
  SmtpClient smtp = new SmtpClient("smtp.gmail.com");
  smtp.EnableSsl = true;
  smtp.UseDefaultCredentials = false;
  smtp.Credentials = new System.Net.NetworkCredential(GmailId, password);
  smtp.Send(mail);
}
 
 
RAJASEKHAR RAJENDRAN replied to Muthu Kumar on 27-Jun-12 04:03 AM
Hi muthu,

Refer the below links, which will solve your issue.

http://stackoverflow.com/questions/9629647/how-to-send-email-from-asp-net-mvc-3 
http://stackoverflow.com/questions/8040204/generating-password-reset-link-in-c-sharp-net 

Thanks & Regards,
Rajasekhar.R