Reset Password by mail

Source: Internet
Author: User
Tags mailmessage smtpclient

  • most of the current user registration password is to use MD5 to implement encryption, and MD5 encryption is indeed irreversible, to decrypt MD5 encrypted data, then use the collision method, but the efficiency is too low, the probability is small, so the user forgets the password can only by resetting the new password (! = Retrieve password)
      • Mail retrieve password prerequisites:
        • The user must verify the mailbox when registering;
      • mail retrieve password process:
        • register a mailbox To send the message to the user (equivalent to the sender);
        • Enter the user name to verify that the user exists (the user name is unique);
        • Enter the mailbox, according to the user name and mailbox to verify that the user's mailbox is correct, if not correct, let the user re-enter, or go down;
        • send link to user mailbox, the link contains key, expiration time;
code implementation (authentication of the user name and verification of the mailbox are omitted here, directly implementing the code to send the message):
    • Implementation of Action:
 PublicJsonresult SendMail () {stringUserName = request.params["UserName"]; stringMail = request.params["Mail"]; //QQ mailbox: [email protected] Get mailbox callout, similar to qq,139,163,126       stringMailsign= Mail. Substring (mail. IndexOf ('@')+1, Mail. IndexOf ('.')-mail. IndexOf ('@')-1); stringMAILADDR =methodutils.getmailaddr (mailsign);//According to the mailbox label, get the corresponding mailbox login address (qq-http://www.mail.qq.com,139-http://www.10086.com )       //determine if the message was sent successfully       if(sendcontent (userName, mail)) {returnJson (New{result ="1", mailaddr =mailaddr}); }      returnJson (New{result ="0" });} 
    • e-Mail content:
 Public BOOLSendcontent (stringUserName,stringemail) {            stringRand =""; Random rampwd=NewRandom (); stringPWD = Rampwd.next (100000,9999999).            ToString (); Rand=pwd; stringKey = rand + userName + email +"Base"; DateTime Time=DateTime.Now;
MD5 encryption key for key= PASSWORDENCRYPTION.GETMD5 (key +Webkey.encryptkey); //Message ContentStringBuilder SB =NewStringBuilder ( -); Sb. Append ("Dear User:<br/><br/>"); Sb. Append ("Click on the link below to set a new password. <br/><br/>"); Sb. Append ("<a href =\ "http://www. Domain name. com/user/resetuserpassword?key="+ key +"&time="+ (time. ToString ("YYYY-MM-DD HH:mm:ss")) +"\ ">http://www. Domain name. com/user/resetuserpassword?key="+ key +"&time="+ (time. ToString ("YYYY-MM-DD HH:mm:ss")) +"</a><br/><br/>"); Sb. Append ("(If you can't click the URL link address, copy it and paste it into the browser's address entry box, and then click Enter.) ) <br/><br/>"); Sb. Append ("Note: Please use <font style= ' Color:red;font-weigth:border ' >1 hours to receive the message, otherwise the link will expire. <br/><br/>"); Sb. Append ("we will, as always, dedicated to serve you! <br/><br/>"); //determine if the message was sent if(!SendMessage (email, sb.) ToString ())) {return false; } userfindpasswordlog userfindpasswordlogentity=NewUserfindpasswordlog () {UserName=UserName, Secretkey=Key, Expirationtime= Convert.todatetime (time. AddHours (1). ToString ("YYYY-MM-DD HH:mm:ss")), Isdel=1};
Save Key,time,username in a link to a databasevarRV = This. BLLController.UserFindPasswordLogBLL.Insert (userfindpasswordlogentity); if(RV. ReturnCode = =0) { return true; } return false; }
    • To send a message:
 Public BOOLSendMessage (stringEmailstringmessagebody) {            stringSMTP ="smtp.163.com"; SmtpClient _smtpclient=NewSmtpClient (); _smtpclient.deliverymethod= Smtpdeliverymethod.network;//specify how e-mail is sent_smtpclient.host = SMTP;//Specify the SMTP server_smtpclient.credentials =NewSystem.Net.NetworkCredential ("(sender's mailbox)","(sender's mailbox password)");//user name and passwordMailMessage _mailmessage =NewMailMessage (); _mailmessage.from=NewMailAddress ((from the sender's mailbox)","(sender name)"); //Recipient_mailmessage.to.add (email); _mailmessage.subjectencoding= System.Text.Encoding.GetEncoding ("gb2312"); _mailmessage.subject="Password Reset";//Theme_mailmessage.body= MessageBody;//content_mailmessage.bodyencoding = System.Text.Encoding.GetEncoding ("gb2312");//Body Code_mailmessage.isbodyhtml =true;//set to HTML format_mailmessage.priority = Mailpriority.high;//priority, high for emergency mail            Try            {
Send mail _smtpclient.send (_mailmessage); } Catch(Exception ex) {//LogLog. Title =Ex. Message; Log. Description=string. Format ("{0}\r\n\tclass:{1},method:{2},line:{3}", ex. ToString (), This. GetType (). Name, System.Reflection.MethodBase.GetCurrentMethod (). Name, Filesutils.getlinenum ()); Log. Create (); return false; } return true; }

Here, e-mail delivery is achieved, but it is worth noting, do not forget to open the Smtp/pop3/imap service email message (163 mailbox as an example)

Reset Password by mail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.