Send mail to springboot
summary
Usage scenario
- Registration verification
- Marketing push
- Trigger mechanism
- Monitoring alarm
Mail Transfer Protocol
- SMTP protocol: the full name is Simple Mail Transfer Protocol. It defines the communication rules between the mail client software and the SMTP mail server, as well as between the two SMTP mail servers.
- POP3 protocol: the full name is Post Office Protocol. It defines the communication rules between mail client software and POP3 mail server.
- IMAP protocol: its full name is Internet Message Access Protocol. It is an extension of POP3 protocol and defines the communication rules between mail client software and IMAP mail server.
Send mail to springboot
Prepare mailbox
Open the service and add an authorization password
Introduce dependency
pom.xml relies on the spring boot starter mail module:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
Add configuration
spring: #mail mail: host: smtp.163.com # Your email 163 username: 163 Send email # Note that the SMTP authorization password is not here password: smtp Authorization code port: 25 protocol: smtp default-encoding: UTF-8
code
Service class
@Slf4j @Service public class MailService { @Value("${spring.mail.username}") private String from; @Autowired private JavaMailSender mailSender; /** * Set the email attachment name not to be intercepted, and solve the problem of garbled Chinese name of the attachment */ public MailService() { System.setProperty("mail.mime.splitlongparameters", "false"); } /** * Simple text mail * * @param to Recipient mail * @param subject Mail subject * @param contnet Mail content */ public void sendSimpleMail(String to, String subject, String contnet) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(contnet); message.setFrom(from); mailSender.send(message); } /** * HTML Text mail * * @param to Recipient mail * @param subject Mail subject * @param contnet HTML content * @throws MessagingException */ public void sendHtmlMail(String to, String subject, String contnet) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo(to); helper.setSubject(subject); helper.setText(contnet, true); helper.setFrom(from); mailSender.send(message); } /** * Attachment mail * * @param to Recipient mail * @param subject Mail subject * @param contnet HTML content * @param filePath Attachment path * @throws MessagingException */ public void sendAttachmentsMail(String to, String subject, String contnet, String filePath) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo(to); helper.setSubject(subject); helper.setText(contnet, true); helper.setFrom(from); FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = file.getFilename(); helper.addAttachment(fileName, file); mailSender.send(message); } /** * Picture mail * * @param to Recipient mail * @param subject Mail subject * @param contnet HTML content * @param rscPath Picture path * @param rscId Picture ID * @throws MessagingException */ public void sendInlinkResourceMail(String to, String subject, String contnet, String rscPath, String rscId) { log.info("Start sending static mail: {},{},{},{},{}", to, subject, contnet, rscPath, rscId); MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = null; try { helper = new MimeMessageHelper(message, true); helper.setTo(to); helper.setSubject(subject); helper.setText(contnet, true); helper.setFrom(from); FileSystemResource res = new FileSystemResource(new File(rscPath)); helper.addInline(rscId, res); mailSender.send(message); log.info("Sending static email succeeded!"); } catch (MessagingException e) { log.info("Failed to send static mail: ", e); } } }
Test class
@RunWith(SpringRunner.class) @SpringBootTest(classes = HealthApplication.class) public class MailServiceTest { @Autowired private MailService mailService; private String toMail = "1048579371@qq.com"; @Test public void sendSimpleMail() { mailService.sendSimpleMail(toMail,"test spring boot imail-theme","test spring boot imail - content"); } @Test public void sendHtmlMail() throws MessagingException { String content = "<html>\n" + "<body>\n" + "<h3>hello world</h3>\n" + "<h1>html</h1>\n" + "<body>\n" + "</html>\n"; mailService.sendHtmlMail(toMail,"This is a letter HTML mail",content); } @Test public void sendAttachmentsMail() throws MessagingException { String filePath = "/Users/liufq/Downloads/ECG customer ranking 2020-08-26 16_26_54.xlsx"; String content = "<html>\n" + "<body>\n" + "<h3>hello world</h3>\n" + "<h1>html</h1>\n" + "<h1>Attachment transmission</h1>\n" + "<body>\n" + "</html>\n"; mailService.sendAttachmentsMail(toMail,"This is a letter HTML mail",content, filePath); } @Test public void sendInlinkResourceMail() throws MessagingException { //TODO changed to local picture directory String imgPath = "/ijiangtao/img/blob/dd9899b4cf95cbf074ddc4607007046c022564cb/blog/animal/dog/dog-at-work-with-computer-2.jpg?raw=true"; String rscId = "admxj001"; String content = "<html>" + "<body>" + "<h3>hello world</h3>" + "<h1>html</h1>" + "<h1>Picture mail</h1>" + "<img src='cid:"+rscId+"'></img>" + "<body>" + "</html>"; mailService.sendInlinkResourceMail(toMail,"This is a picture email",content, imgPath, rscId); } }
deploy
In the production environment, the general mail service will be deployed separately and exposed through HTTP or MQ.
problem
Solution of Chinese garbled code of attachment display name
Reason: if the length of the encoded file name is greater than 60, the value of splitLongParameters is true, and the value of encodeParameters is true, the file name will be intercepted
Add settings to the structure of the service, or add settings to the main startup class
public MailService() { System.setProperty("mail.mime.splitlongparameters", "false"); }
550 User has no permission
smtp authorization is enabled for the sender's mailbox
reference material
https://blog.csdn.net/sinat_26342009/article/details/89425836