Java-user activation email Tool
We often encounter a need to send an activation email to our registered email address when registering a new user on the website or software. Then, we can log on to our user name after clicking "Activate connection" in the email address, the process is when the database has already stored the user's information after successful registration, but the user status is unavailable, so this user name cannot be used normally at this time. Therefore, the system needs to send an activation email to our registered email address. After we click "Activate connection", the system will change the user status field in the database to available. Now, the user is successfully activated, this user can be used normally. The implementation process is as follows:
For convenience, we need to write a mail tool class.
Package cn. itcast. shop. utils; import java. util. properties; import javax. mail. authenticator; import javax. mail. message; import javax. mail. message. recipientType; import javax. mail. messagingException; import javax. mail. passwordAuthentication; import javax. mail. session; import javax. mail. transport; import javax. mail. internet. addressException; import javax. mail. internet. internetAddress; import javax. mail. internet. mimeMessage;/*** mail sending tool class * @ author shx **/public class MailUitls {/*** method of sending mail * @ param to: Recipient * @ param code: activation code */public static void sendMail (String to, String code) {/*** 1. obtains a Session object. * 2. create an object Message that represents the email. * 3. send the email Transport * // 1. obtain the connection object Properties props = new Properties (); props. setProperty (mail. host, localhost); Session session = Session. getInstance (props, new Authenticator () {@ Overrideprotected PasswordAuthentication getPasswordAuthentication () {return new PasswordAuthentication (service@shop.com, 111) ;}}); // 2. create an email object: Message message = new MimeMessage (session); // set the sender: try {message. setFrom (new InternetAddress (service@shop.com); // sets the recipient: message. addRecipient (RecipientType. TO, new InternetAddress (to); // cc bcc // set the title message. setSubject (from the official activation email); // set the mail body: message. setContent (
Official activation email! Click the following link to complete activation! Http: // 192.168.24.162: 8080/shop/user_active.action? Code = + code +, text/html; charset = UTF-8); // 3. send Email: Transport. send (message);} catch (AddressException e) {e. printStackTrace ();} catch (MessagingException e) {e. printStackTrace () ;}} public static void main (String [] args) {sendMail (aaa@shop.com, 11111111111111) ;}} Action:
/***** User registration */public String regist () {userService. save (user); this. addActionMessage (registration successful, fun mailbox activated !); Return msg;}/*** method of User activation */public String active () {// query User by activation code: User existUser = userService. findByCode (user. getCode (); // determines if (existUser = null) {// this. addActionMessage (activation failed: activation code error !);} Else {// activated successfully // modify the user's status existUser. setState (1); existUser. setCode (null); userService. update (existUser); this. addActionMessage (activated successfully: Log On !);} Return msg ;}
Service:
// Complete the User registration code at the business layer: public void save (user) {// save the data to the database User. setState (0); // 0: indicates that the user is not activated. 1: indicates that the user has been activated. string code = UUIDUtils. getUUID () + UUIDUtils. getUUID (); // call the random ID generation tool user. setCode (code); userDao. save (user); // send the activation email; MailUitls. sendMail (user. getEmail (), code);} // The service layer queries the User's public User findByCode (String code) {return userDao Based on the activation code. findByCode (code);} // Method for modifying the User's status public void update (User existUser) {userDao. update (existUser );}
Dao:
Public void save (User user) {// TODO Auto-generated method stubthis. getHibernateTemplate (). save (user) ;}// query the User's public User findByCode (String code) {String hql = from user where code =? Based on the activation code ?; List
List = this. getHibernateTemplate (). find (hql, code); if (list! = Null & list. size ()> 0) {return list. get (0) ;}return null ;}// Method for modifying the User State public void update (User existUser) {this. getHibernateTemplate (). update (existUser );}
Database activated after successful registration
Database after activation