Forget your password and retrieve it via email

Source: Internet
Author: User
Tags email account

Solve the user forgot password, by sending email back

@Action ("send") public String SendMail () {    accountservices.sendforgetmail (email);     return SUCCESS;}

//through the database query input box email account, with this account and then really send,//the properties of the online thread are written as constants     Public voidSendforgetmail (FinalString Email) { Account Account=accountdao.findbyemail (email); if(Account! =NULL) {            /*in the message sent, the mailbox of the account to which the password was retrieved, and token token are unique, prevent the URL from being modified to change the password*/            FinalString token =Uuid.randomuuid (). toString (); /*put in cache and expire after 5 minutes*/Cache.put ("Forget:" +email, 300, token); /*E -Mail is a process, can not wait to send out in the jump, will send into a thread*/Thread Thread=NewThread (NewRunnable () { Public voidrun () {Htmlemail mail=NewHtmlemail (); //setting the host server addressMail.sethostname ("smtp.126.com"); Mail.setauthentication ("Kaishengit", "[email protected] @rd"); //whether validation is requiredMail.settls (true); Mail.setcharset ("UTF-8"); Try {                    //SenderMail.setfrom ("[Email protected]"); //ThemeMail.setsubject ("Password Retrieve mail"); //contentMail.sethtmlmsg ("Click this link to recover password: <a href=\" http://localhost/forget/validate.action?email= "+email+" &token= "+ token+ "\" > Reset Password </a> ");                        Mail.addto (email);                                            Mail.send (); } Catch(emailexception e) {e.printstacktrace ();                        }                }            });        Thread.Start (); }            }

//Cache written as tool class Public classCache {Private StaticMemcachedclient client =buildeclient (); Private Staticmemcachedclient buildeclient () {Try {            return NewMemcachedclient (addrutil.getaddresses ("127.0.0.1:11211")); } Catch(IOException e) {Throw NewRuntimeException ("Memcachedclient get Error", E); }    }         Public Static voidPut (String key,intTime,object value)    {Client.add (key, time, value); }         Public StaticObject get (String key) {returnClient.get (key); }         Public Static voidRemove (String key) {client.delete (key); }        }

Click Retrieve password in the mailbox

@Action ("Validate")public = Accountservices.validatemailandtoken (email,token);     if NULL ) {        return  INPUT;    }     return SUCCESS;}

//Service in PublicAccount Validatemailandtoken (string email, string token) {string Cachetoken= (String) cache.get ("Forget:" +email); if(Cachetoken = =NULL) {System.out.println ("Can't Get Token"); return NULL; } Else {            if(Token.equals (Cachetoken)) {Cache.remove ("Forget:" +email); returnaccountdao.findbyemail (email); } Else{System.out.println ("Two tokens Inconsistent"); return NULL; }        }    }

Forget your password and retrieve it via email

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.