赞
踩
经常遇到需要发送带附件的邮件的功能,特写一份详细的笔记
<!--发送邮件的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.5.4</version>
</dependency>
<!--thymeleaf 邮件模板依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.5.4</version>
</dependency>
邮件模板放在 resource/templates路径下
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>template</title> <style> p { font-size: larger; font-weight: bold; } #yes { padding: 10px; font-size: 16px; border: none; border-radius: 5px; background-color: orange; color: white; } #no { padding: 10px; font-size: 16px; border: none; border-radius: 5px; background-color: darkgrey; color: white; } div { width: 100%; margin: 0 auto; } div.offer-info { padding: 10px 10px 0; border-top: 1px solid #ccc; color: #747474; margin-bottom: 20px; line-height: 1.3em; font-size: 12px; } #note,#note1{ font-weight: bold; } </style> </head> <body> <p>XXXXXX</p> <span th:text="${userName}"></span>,您好 <br> <br> <div>请查收XXXXXX</div><br> <span id="note">您的XXXXXX</span><br><br> <span id="note1" style="color: orange">请点击XXXX</span><br><br> <button id="no">A</button> <button id="yes">B</button> <br> <br> <div class="offer-info"> <br> <p>此为系统邮件,请勿回复<br>Please do not reply to this system email</p> </div> </body> </html>
offerMail:
host: mail.XXXXX.com
port: 25
sender: XXXXXX@XXXX.com
password: XXXXXXXX
@Service public class Template { @Autowired private TemplateEngine templateEngine; private JavaMailSenderImpl javaMailSender = null; public void sendThymeleafMail() throws Exception { JavaMailSender javaMailSender = getJavaMailSenderParam(); MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setSubject("邮件标题"); helper.setFrom("XXXXX@XX.com"); helper.setTo("XXXX@qq.com"); helper.setSentDate(new Date()); // 绑定到mail.thymeleaf模板的数据 Context context = new Context(); context.setVariable("userName", "Tom"); String process = templateEngine.process("mail.html", context); // setText()第二个参数:开启html模板 helper.setText(process, true); // 添加附件 TODO File emailFile = new File(); helper.addAttachment("XXXX.PDF", emailFile); javaMailSender.send(mimeMessage); } } private JavaMailSender getJavaMailSenderParam() { if (javaMailSender != null) { return javaMailSender; } System.getProperties().setProperty("mail.mime.splitlongparameters", "false"); javaMailSender = new JavaMailSenderImpl(); Properties properties = new Properties(); properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); javaMailSender.setJavaMailProperties(properties); javaMailSender.setPort(appConfig.getOfferMail().getPort()); javaMailSender.setUsername(appConfig.getOfferMail().getSender()); javaMailSender.setPassword(appConfig.getOfferMail().getPassword()); javaMailSender.setDefaultEncoding("UTF-8"); javaMailSender.setHost(appConfig.getOfferMail().getHost()); return javaMailSender; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。