1

How can I make this code send a new generated password or the user its old password?

I figured out how to let the code send a email! The sender is not the problem but the receiver is. Where EmailAdresNaar must be replaced with an email that someone puts into a text box or something.

public void SendEmail()
    {



        string emailAdresVan = "invoerenSVP";
        string password = "invoerenSVP";


        MailMessage msg = new MailMessage();

        msg.From = new MailAddress(emailAdresVan);
        msg.To.Add(new MailAddress(EmailAdresNaar));
        msg.Subject = Onderwerp;
        msg.Body = Bericht;
        msg.IsBodyHtml = true;


        SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);

        NetworkCredential loginInfo = new NetworkCredential(emailAdresVan, password);


        smtpClient.EnableSsl = true;
        smtpClient.UseDefaultCredentials = true;
        smtpClient.Credentials = loginInfo;

        smtpClient.Send(msg); 

       }
   }
}
olly_uk
  • 11,559
  • 3
  • 39
  • 45
  • use `YourTextBoxControlID.Text` to get the value user entered in the textbox and use that. – Shyju Apr 02 '13 at 13:15
  • Reset procedure shouldnt send new password or any password at all. http://stackoverflow.com/questions/2734367/implement-password-recovery-best-practice http://stackoverflow.com/questions/1102781/best-way-for-a-forgot-password-implementation – adt Apr 02 '13 at 13:15

1 Answers1

1

Put data you need like this:

msg.From = new MailAddress("support@yoursite.com");
msg.To.Add(new MailAddress(UserEmailTextBox.Text));
msg.Subject = "New Password";
msg.Body = GenerateNewPassword();

and example of method GenerateNewPassword(), but you should make it much more complex to return randomly generated new password (you need to google for different variations of implementation):

public string GenerateNewPassword()
{
    string new_pass = "";
    // generate new password
    return new_pass;
}

Ideally, you should make another page like PasswordsReset.aspx, and send to user link to this page with some kind of GUID, where user can create new password by himself.

Eugene Pavlov
  • 688
  • 5
  • 18
  • But The user already has an account with a registerd email, i somehow need to tell my emailsender that he resets the password that is linked to that email that he or she puts into the text box. – user2235949 Apr 02 '13 at 13:37
  • This is businesses logic you need to define by yourself. You see now why you will need `PasswordsReset.aspx` - to not allow any users to reset other users passwords. You need to send user GUID, save it to database, and after user follow link and resets password, you save new password to DB in code-behind to one that user provide on PasswordsReset page. – Eugene Pavlov Apr 02 '13 at 13:52