Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement automatic email sending task with springboot

Shulou Source: shulou.com Published: 2022-06-01 00:14:41 09月21日 Update

Most people do not understand the knowledge points of this article "springboot how to achieve automatic email sending task", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "springboot how to achieve automatic email sending task" article.

1. Import jar package org.springframework.boot spring-boot-starter-mail 2.5.22. Configuration file

Get QQ Mail's POP3/SMTP service, get the encrypted password, and write the configuration file of springboot

Spring.mail.username=2080038188@qq.com # QQ is not mine.

Spring.mail.password=gzcmhxxybgvbcjil # here is the encrypted password

Spring.mail.host=smtp.qq.com

Spring.mail.properties.mail.smtp.ssl.enable=true # this step is to turn on encryption authentication

3. test

Test in a test class

@ SpringBootTestclass Sprintboot09TestApplicationTests {@ Autowired JavaMailSenderImpl mailSender; @ Test void contextLoads () {SimpleMailMessage mailMessage = new SimpleMailMessage (); mailMessage.setSubject (Hello); mailMessage.setText (thank you); mailMessage.setTo (2080038188@qq.com); mailMessage.setFrom (2080038188@qq.com); mailSender.send (mailMessage);}}

Then send it successfully, your QQ Mail will receive your own message, here is a great sense of achievement!

Key: in my test, there has been bug, looking for nearly half an hour to find, the result is that our springboot configuration file hit an extra space, the naked eye can not see, this kind of error, it is true to find good hard, once suspected is the jar package version or QQ Mail problem, the result is too much worry. Be sure to pay attention to the space problem in the springboot configuration file in the future!

Complex email send @ Testvoid contextLoads2 () throws MessagingException {/ / A complex email MimeMessage mimeMessage = mailSender.createMimeMessage (); / / assemble MimeMessageHelper helper = new MimeMessageHelper (mimeMessage, true); / / body helper.setSubject ("Hello ~ plus"); helper.setText ("Thank you for your crazy talk java series course ~

", true); / / Annex helper.addAttachment (" 1.jpg ", new File (" C:\\ Users\\ 86187\ Desktop\\ 1.jpg "); helper.setTo (" 2080038187@qq.com "); helper.setFrom (" 2080038187@qq.com "); mailSender.send (mimeMessage) } the above is the content of this article on "how to achieve automatic email sending task in springboot". I believe you all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Email content files testing configuration tasks that is mailboxes encryption complexity OK passwords articles knowledge spaces articles results questions success value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information Huawei Shulou Tech Info Microsoft