Analysis of the process of Integration of JavaMail and Spring
This article introduces the relevant knowledge of "the process analysis of the integration of JavaMail and Spring". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Brief introduction
After the integration of javaMail and spring is completed, the efficiency of sending mail can be greatly increased. As soon as the server starts, the configuration file is loaded. When saving user information directly, the mail can be sent directly, which greatly improves the efficiency.
1. Introduction of coordinates
Javax.mail mail 1.4.4 org.springframework spring-context-support 4.2.4.RELEASE
two。 Extract MailUtils tool classes
Package cn.itcast.jk.utils;import java.util.Properties;import javax.mail.Address;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMessage.RecipientType;public class MailUtils {/ * send email * @ throws Exception * * / public static void sendMsg (String toAddress,String subject,String content) throws Exception {/ / 1. Set some information of the mail Properties props = new Properties (); / / send the mail to the server address props.put ("mail.smtp.host", "smtp.163.com"); / / smtp.qq.com stmp.sina.com props.put ("mail.smtp.auth", "true"); / / 2. Create a Session object Session session = Session.getInstance (props); / / 3. Create a MimeMessage, the message object of the mail MimeMessage message = new MimeMessage (session); / / 4. Set sender Address fromAddr = new InternetAddress ("sender mailbox"); message.setFrom (fromAddr); / / 5. Set recipient Address toAddr=new InternetAddress (toAddress); message.setRecipient (RecipientType.TO, toAddr); / / 6. Set the subject of the message message.setSubject (subject); / / 7. Set the body of the message message.setText (content); message.saveChanges (); / / Save updates / / 8. Get the rocket Transport transport = session.getTransport ("smtp"); transport.connect ("smtp.163.com", "sender mailbox", "sender password"); / / set the launch address of the rocket transport.sendMessage (message, message.getAllRecipients ()); / / send the specific content and the recipient transport.close ();}}
3.mailproperties.xml
Mail.host=smtp.163.commail.username= your email name-- that is, @ the thing in front of mail.password= password mail.from= your email address
4. Create applicationContext-mail.xml
Configuration file true true 0 for JavaMail
5.applicationContext.xml introduces applicationContext-mail.xml
Note value in 6.applicationContext-service.xml
MailMessage and mailSender are injected into UserServiceImpl
Add to the saveorUpdate method in the 7.UserServiceImpl class
/ spring integration javaMail needs to be injected: private SimpleMailMessage mailMessage; private JavaMailSender mailSender; public void setMailMessage (SimpleMailMessage mailMessage) {this.mailMessage = mailMessage;} public void setMailSender (JavaMailSender mailSender) {this.mailSender = mailSender;} public void saveOrUpdate (final User entity) {if (UtilFuns.isEmpty (entity.getId () {/ / determine whether id has a value / / indicates that id has no value, indicating that entity.setState (1) is saved. / / 1 means String id = UUID.randomUUID () .toString (); entity.setId (id); entity.getUserinfo () .setId (id); / / to set the initial password, you need to encrypt the default password and save it to the database entity.setPassword (Encrypt.md5 (SysConstant.DEFAULT_PASS, entity.getUserName (). / / final is to extend the life cycle of the object, otherwise entity can only be used in saveOrUpdate. After the completion of the method, the stack is popped up, and the previously defined entity cannot be used in the run method. / / using spring and javaMail to send mail to new employees / / the purpose of using threads and try-catch is that if mail delivery fails, the information will not be saved to the database. Sending mail has become a separate process. Thread th = new Thread (new Runnable () {public void run () {try {mailMessage.setTo (entity.getUserinfo (). GetEmail ()); mailMessage.setSubject ("New employee onboarding Information") MailMessage.setText ("Welcome" + entity.getUserinfo (). GetName () + "join Langfang Sichuangzhiyuan Technology Co., Ltd., your account number:" + entity.getUserName () + ", password:" + SysConstant.DEFAULT_PASS); mailSender.send (mailMessage);} catch (MailException e) {e.printStackTrace ();}; th.start () } baseDao.saveOrUpdate (entity);} "process Analysis of the Integration of JavaMail and Spring" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!