In many websites registration, in order to verify the authenticity of the user information, often need to verify the accuracy of the user-filled messages. Form: When the user registers to fill in the mailbox, after the registration completes, the website will send the user to fill the mailbox sends an activation mail, the user clicks activates the link in the mail, can complete the registration.
Recent projects also used this demand, did a demo to share with you, as big as the idea is as follows:
database table Structure
User table T_user has five fields for user name, password, email address, activation code, Status:
| Username | password | Email |code | State |
Core code:
Usermanager.java
ImportJava.util.Properties;ImportJava.util.UUID;ImportJavax.mail.Authenticator;ImportJavax.mail.Message;ImportJavax.mail.MessagingException;ImportJavax.mail.PasswordAuthentication;ImportJavax.mail.Session;ImportJavax.mail.Transport;ImportJavax.mail.Message.RecipientType;ImportJavax.mail.internet.AddressException;Importjavax.mail.internet.InternetAddress;ImportJavax.mail.internet.MimeMessage;ImportDao. Userdao; Public class usermanager { /** * Registered user * @param username user name * @param password Password * @param Email address * @return Registration is successful */ PublicBooleanRegister(String username,string password,string email) {Boolean result=false;//Generate user codeString Code=uuid.randomuuid (). toString (). Replace ("-","");//Add UsersUserdao userdao=NewUserdao (); Result=userdao.adduser (username, password, email, code);//Send activation message to userSendMail (Email,code);returnResult }/** * Send activation email * @param to Recipient's email address * @param Code Activation Code * * Public Static Boolean SendMail(String to, string code) {Try{Properties props =NewProperties (); Props.put ("username","[email protected]"); Props.put ("Password","123456"); Props.put ("Mail.transport.protocol","SMTP"); Props.put ("Mail.smtp.host","Smtp.163.com"); Props.put ("Mail.smtp.port","very"); Session mailsession = session.getdefaultinstance (props); Message msg =NewMimeMessage (mailsession); Msg.setfrom (NewInternetAddress ("[email protected]")); Msg.addrecipients (Message.RecipientType.TO, Internetaddress.parse (to)); Msg.setsubject ("Activate Mail"); Msg.setcontent ("+code+">http://localhost:8080/SendMail/servlet/ActiveServlet</a>,"Text/html;charset=utf-8"); Msg.savechanges (); Transport Transport = Mailsession.gettransport ("SMTP"); Transport.connect (Props.getproperty ("Mail.smtp.host"), props. GetProperty ("username"), Props.getproperty ("Password")); Transport.sendmessage (MSG, msg.getallrecipients ()); Transport.close (); }Catch(Exception e) {E.printstacktrace (); System.out.println (e);return false; }return true; }/** * Activate user * @param Code User Activation Code * @return activation Successful */ PublicBooleanActive(String code) {Userdao userdao=NewUserdao (); String Username=userdao.finduserbycode (code);if(username!=NULL&& username!=""){//If there is a user, set this user state to availableUserdao.setstate (username);return true; }Else{return false; } }}
register.jsp
When the user registers
1, for the user to generate a random activation code (this activation code is added according to code rules automatically generated, here the activation code with a randomly generated UUID for example);
2, add a user record;
3. Send the activation email to the user. The sender of the above code is a small series of their own mailbox, generally for enterprise mailbox. If there are conditions, it is also recommended to you a mailbox server-e-mail server, you can set up a mail system within the LAN to test.
The activation message is as follows:
Activated
After the user clicks the link in the mailbox, the system queries the user record that matches the activation code according to the activation code sent by the user clicking the link, and if the record exists, the user state is changed to the available state.
(Confined to the space, here do not post all the code, the need for friends can click this download source code)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java implementation when registering to send activation Mail + activation