SpringBoot-Thymeleaf发送模板带附件邮件

2019-04-13 17:03发布

SpringBoot-Thymeleaf发送模板带附件邮件

自个学习笔记不喜勿喷

@RunWith(SpringRunner.class) @SpringBootTest public class SpringBootEmailApplicationTests { @Value("${spring.mail.username}") private String fromEmailAddr; @Autowired private JavaMailSender javaMailSender; @Autowired private TemplateEngine templateEngine; /** * @throws MessagingException * @Title: sendHtmlEmail * @Description: 发送thymeleaf模板邮件 * @return void * @throws */ @Test public void sendHtmlEmail() throws MessagingException { MimeMessage mailMessage = javaMailSender.createMimeMessage(); //开启带附件true MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true); IContext context = new Context(); //获取模板html代码 String process = templateEngine.process("index", context); try { messageHelper.setFrom(fromEmailAddr); messageHelper.setTo("112121@qq.com"); messageHelper.setSubject("SpringBootThymeleaf模板邮件"); messageHelper.setText(process, true); FileSystemResource avatar = new FileSystemResource( new File("F:\workspace\SpringBootEmail\src\main\resources\templates\img\bappy.jpg")); // messageHelper.addInline("avatar", avatar); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } javaMailSender.send(mailMessage); } } 有一个问题,那就是不能发送外嵌样式的邮件,只能将样式弄进模板文件里。