When we usually go online, we often use website registration. There are many methods, such as sending text messages to verify and sending email messages. Recently, under the SSH framework, a simple email activation link was made to the mailbox and then activate a function of registering users. The MailServer I am using is an easy mail mail server, which refers to the javax.mail.jar package. As the name suggests, javamail provides developers with programming interfaces related to processing emails. It is an API released by Sun to handle email. It can conveniently perform some commonly used mail transfers.
【Common categories introduction】
Here are some commonly used classes, let me introduce them to you:
【Authenticator】
This is an abstract class that accesses protected resources through username and password, which are mail servers. After creation is complete, the Authenticator must be registered with the session. Then, when authentication is required, the Authenticator will be notified.
【Message】
This is an abstract class to create messages to be sent, such as topics and content. There must be a subclass to implement the method.
【Session】
The Session class defines a basic mail session, which is the highest-level entry class of Java Mail API. All other classes are only effective through this session. Session objects use Java.util.Properties objects to obtain information, such as mail server, username, password and other information shared throughout the application. This Session class represents a mail session in JavaMail. Each JavaMail-based application has at least one session but can have as many sessions as possible.
Session sendMailSession;
sendMailSession = Session.getInstance(props, null);
【Transport】
The last part of message sending is to use the Transport class. This class sends messages in the language specified by the protocol (usually SMTP). It is an abstract class, and it works somewhat similarly to Session. By calling the static send() method, the default version of the class can be used: Transport.send(message). Alternatively, readers can get a specific instance from a session against their own protocol, pass the username and password (not if not necessary), send a message, and then close the connection.
【Code Implementation】
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.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 Zhouzhou * @date 2016-1-1 * @time 08:52:47 am */ public class MailUitls { public static void sendMail(String to,String code){ /** * 1. Get session * 2. Create a code mail object message * 3. Send email Transport */ /** * 1. Get connection object*/ Properties props=new Properties(); props.setProperty("mail.host","localhost"); Session session=Session.getDefaultInstance(props, new Authenticator(){ @Override protected PasswordAuthentication getPasswordAuthentication() { // TODO Auto-generated method stub return new PasswordAuthentication("[email protected]","111"); } }); //2. Create a mail sending object Message message=new MimeMessage(session); //3. Set the sender try { message.setFrom(new InternetAddress("[email protected]")); //Set the recipient message.addRecipient(RecipientType.TO, new InternetAddress(to)); //Title message.setSubject("Official activation email from Zhouzhou Mall"); message.setContent("<h1>AZZ Mall official activation email! Click the link below to complete the activation operation!</h1><h3><a href='http://192.168.21.114:8080/shop/user_active.action?code="+code+"'>http://192.168.21.114:8080/shop/user_active.action?code="+code+"</a></h3>", "text/html;charset=UTF-8"); // 3. Send email: Transport.send(message); } catch (AddressException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { sendMail("[email protected]","Your activation code is azz19931016"); } } The User's business layer calls this class when registering and uses the sendmail method.
package cn.itcast.shop.user.service; import org.springframework.transaction.annotation.Transactional; import cn.itcast.shop.user.dao.UserDao; import cn.itcast.shop.user.vo.User; import cn.itcast.shop.utils.MailUitls; import cn.itcast.shop.utils.UUIDUtils; //Business layer @Transactional public class UserService { //Installing the method to query the user by username private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public User findByUsername(String username){ return userDao.findByUsername(username); } //Business layer: Complete user registration public void save(User user) { // TODO Auto-generated method stub //Save data in the database user.setState(0);//1 has been activated, 0 has not been activated String code=UUIDUtils.getUUID()+UUIDUtils.getUUID(); user.setCode(code); userDao.save(user); // Send activation email; MailUitls.sendMail(user.getEmail(), code); } // The business layer querys the user based on the activation code public User findByCode(String code) { return userDao.findByCode(code); } // Method to modify the user's status public void update(User existUser) { userDao.update(existUser); } // Method to log in public User login(User user) { return userDao.login(user); } }Finally, this effect was achieved:
The above is all about this article, I hope it will be helpful to everyone's learning.