In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to use Springboot to send mail". In the daily operation, I believe that many people have doubts about how to use Springboot to send mail. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Springboot to send mail". Next, please follow the editor to study!
The details are as follows
Chapter one background introduction
1.1 usage scenarios
1. Registration verification
2. Website marketing
3. The last line of defense of safety
4. Reminders, monitoring warnings
5. Trigger mechanism.
1.2 principle of sending mail
1. Mail transfer Protocol: SMTP Protocol and POP3 Protocol
two。 Evolving content: IMAP and Mme protocols
1.3 Mail delivery proc
Chapter 2 using SpringBoot to complete email delivery
2.1 Development process
2.2 develop simple text mail
2.2.1 introducing related jar packages
Add dependencies in pom.xml
Org.springframework.boot spring-boot-starter-mail
2.2.2 configure mailbox parameters
Configure in the configuration file: the password here is the authorization code, not the password on the web page.
Spring.mail.host=smtp.163.comspring.mail.username=XXX@163.comspring.mail.password=XXXspring.mail.default-encoding=utf-8
2.2.3 encapsulated SimpleMailMessage
SimpleMailMessage message = new SimpleMailMessage ()
2.2.4 send by JavaMailSender
@ Autowiredprivate JavaMailSender mailSender;// uses JavaMailSender to send mail mailSender.send (message)
Specific implementation:
/ * * @ Description: send an email * @ Author: yzy * @ Date: 14:01 on 2021-10-19 * * / @ Servicepublic class MailService {@ Value ("${spring.mail.username}") private String sendPeople; @ Autowired private JavaMailSender mailSender / * * @ Description: send the text file * @ author: yzy * @ date: 14:01 on 2021-10-19 * @ Param: * @ return: * / public void sendSimpleMail (String to,String subject,String content) {SimpleMailMessage message = new SimpleMailMessage (); / / recipient message.setTo (to) / / subject message.setSubject (subject) to send messages; / / message.setText to send messages (content); / / sender message.setFrom (sendPeople); / / mailSender.send to send messages using JavaMailSender (message);}}
Test:
Package com.yzy.restaurant.mapper;import com.yzy.restaurant.MailService;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith (SpringRunner.class) @ SpringBootTestpublic class MailTest {@ Autowired private MailService mailService @ Test public void sendSimpleMailTest () {mailService.sendSimpleMail ("yzy20162362@163.com", "this is a simple demo", "ha, it was sent successfully!") ;}}
Start:
Effect:
2.3 develop HTML mail
Add the code in MailService and MailTest
/ * * @ Description: send html to * @ author: yzy * @ date: 2021-10-19 14:58 * @ Param: * @ return: * / public void sendMailHtml (String to,String subject,String content) throws MessagingException {MimeMessage message = mailSender.createMimeMessage (); MimeMessageHelper helper = new MimeMessageHelper (message,true); helper.setTo (to) Helper.setSubject (subject); helper.setText (content,true); helper.setFrom (sendPeople); mailSender.send (message) } / * * @ Description: send html to * @ author: yzy * @ date: 2021-10-19 14:58 * @ Param: * @ return: * / public void sendMailHtml (String to,String subject,String content) throws MessagingException {MimeMessage message = mailSender.createMimeMessage (); MimeMessageHelper helper = new MimeMessageHelper (message,true); helper.setTo (to) Helper.setSubject (subject); helper.setText (content,true); helper.setFrom (sendPeople); mailSender.send (message);}
2.3 develop attachment messages
Add the code in MailService and MailTest
/ * * @ Description: send an attachment email * @ author: yzy * @ date: 15:12 on 2021-10-19 * @ Param: * @ return: * / public void sendAttachmentsMail (String to,String subject,String content,String filePath) throws MessagingException {MimeMessage message = mailSender.createMimeMessage (); MimeMessageHelper helper = new MimeMessageHelper (message,true); helper.setTo (to) Helper.setSubject (subject); helper.setText (content,true); helper.setFrom (sendPeople); / / read FileSystemResource file = new FileSystemResource (new File (filePath)); / / get the file name String filename = file.getFilename (); / / set attachment helper.addAttachment (filename,file); / / send mailSender.send (message) @ Test public void sendAttachmentsMailTest () throws MessagingException {String filePath = "work summary of the third week of March 2020 (March 16-March 20) (1) .xlsx"; mailService.sendAttachmentsMail ("yzy20162362@163.com", "this is an attachment message", "ha, attachment mail was sent successfully", filePath);}
2.4 Picture Mail
Add the code in MailService and MailTest
/ * * @ Description: email with pictures * @ author: yzy * @ date: 15:35 * @ Param: * @ return: * / public void sendPhotoMail (String to,String subject,String content,String rscPath, String rscId) throws MessagingException {MimeMessage message = mailSender.createMimeMessage (); MimeMessageHelper helper = new MimeMessageHelper (message,true) Helper.setTo (to); helper.setSubject (subject); helper.setText (content,true); helper.setFrom (sendPeople); / / read FileSystemResource rec = new FileSystemResource (new File (rscPath)); helper.addInline (rscId,rec); / / send mailSender.send (message) } @ Test public void sendPhotoMailTest () throws MessagingException {String imgPath = "C:\ Users\\ yzy\\ Desktop\\ Wechat picture _ 20210917201828.jpg"; String rsc = "0001"; mailService.sendPhotoMail ("yzy20162362@163.com", "this is a picture email", "ha, picture email was sent successfully", imgPath,rsc);}
2.5 Mail template
Template mail is especially suitable for:
1. Email registered by the user; 2. An email that forgets its password
I use thymeleaf, as long as themleaf is configured and coded.
Create the page of the new emailTemplate:
Hello mail template, thank you for your registration, this is a verification email, please click the link below to complete the registration, thank you for your support!
Activate account >
Test the code:
@ Test public void sendTemplateMailTest () throws MessagingException {Context content = new Context (); content.setVariable ("id", "111l"); String emailContent = templateEngine.process ("emailTemplate", content); mailService.sendMailHtml ("yzy20162362@163.com", "this is a template email", emailContent);} at this point, the study on "how to use Springboot to send email" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.