Java-user-activated email tools and java email tools
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.
<Span style = "font-family: KaiTi_GB2312; font-size: 18px; color: #333333;"> 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; I Mport 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 "); Sess Ion 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 ());// Cc bcc // set the title message. setSubject ("from the official activation email"); // set the mail body: message. setContent ("Action:
<Span style = "font-family: KaiTi_GB2312; font-size: 18px; color: #333333;">/***** user registration */public String regist () {userService. save (user); this. addActionMessage ("registration successful, fun mailbox activated! "); Return" msg ";}/*** User activation method */public String active () {// query the User based on the activation code: User existUser = userService. findByCode (user. getCode (); // determines if (existUser = null) {// this. addActionMessage ("activation failed: activation code error! ");} Else {// activation successful // modify the user's status existUser. setState (1); existUser. setCode (null); userService. update (existUser); this. addActionMessage ("activated successfully: Log On! ");} Return" msg ";}</span>
Service:
<Span style = "font-family: KaiTi_GB2312; font-size: 18px; color: #333333;"> // The service layer completes User registration code: public void save (user User user) {// store 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) ;}</span>
Dao:
<Span style = "font-family: KaiTi_GB2312; font-size: 18px; color: #333333;"> 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 =? "; List <User> 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) ;}</span>
Database activated after successful registration
Database after activation
Because we only want the user to activate it once, after successful activation, the data code field is cleared and the state is changed to 1. At present, the system is not online. To facilitate the test, I use a local mailbox to test and only support LAN. For more information about the Internet, see Blog Tips!
Copyright Disclaimer: this is an original article by the blogger. Please mark the source for reprinting. Thank you!