package com.example.demo;

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.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.context.IContext; import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage; @RunWith(SpringRunner.class)
@SpringBootTest
public class SendMailWithTemplate {
@Autowired
JavaMailSender javaMailSender; @Autowired
TemplateEngine templateEngine; @Test
public void sendMailWithTempalte() throws MessagingException {
MimeMessage mailMessage = javaMailSender.createMimeMessage();
//开启带附件true
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true);
// 声明一个上下文对象,里面放入要存到模板里面的数据
IContext context = new Context();
//获取模板html代码
((Context) context).setVariable("username", "alex");
// 指定相应的模板,然后给context数据传过去
String process = templateEngine.process("index", context); try {
messageHelper.setFrom("wangzhilei@jd.com");
messageHelper.setTo("wangzhilei@jd.com");
// 抄送
// String[] mails= new String[]{};
// messageHelper.setCc(mails);
// 密送
// messageHelper.setBcc();
messageHelper.setSubject("tempalte 模板邮件");
// 设置处理好的结果,本质就是读取内容然后 后面一个参数html:true必须为true,保证正常的读取hml messageHelper.setText(process, true);
// 发送模板的时候,同时带附件过去
FileSystemResource fileSystemResource = new FileSystemResource("src/main/resources/static/images/a.jpg");
messageHelper.addAttachment("b.jpg", fileSystemResource);
} catch (MessagingException e) {
e.printStackTrace();
} javaMailSender.send(mailMessage);
}
}

  

springboot 发送邮件+模板+附件的更多相关文章

  1. SpringBoot 发送邮件和附件

    作者:yizhiwaz 链接:www.jianshu.com/p/5eb000544dd7 源码:https://github.com/yizhiwazi/springboot-socks 其他文章: ...

  2. 1.使用javax.mail, spring的JavaMailSender,springboot发送邮件

    一.java发邮件 电子邮件服务器:这些邮件服务器就类似于邮局,它主要负责接收用户投递过来的邮件,并把邮件投递到邮件接收者的电子邮箱中,按功能划分有两种类型 SMTP邮件服务器:用户替用户发送邮件和接 ...

  3. SpringBoot thymeleaf模板页面没提示,SpringBoot thymeleaf模板插件安装

    SpringBoot thymeleaf模板插件安装 SpringBoot thymeleaf模板Html页面没提示 SpringBoot  thymeleaf模板页面没提示 SpringBoot t ...

  4. SpringBoot 发送邮件功能实现

    背景 有个小伙伴问我你以前发邮件功能怎么弄的.然后我就给他找了个demo,正好在此也写一下,分享给大家. 理清痛点 发送邮件,大家可以想一下,坑的地方在哪? 我觉得是三个吧. 第一:邮件白名单问题. ...

  5. SpringBoot thymeleaf模板版本,thymeleaf模板更换版本

    SpringBoot thymeleaf模板版本 thymeleaf模板更换版本 修改thymeleaf模板版本 ================================ ©Copyright ...

  6. jenkins 发送邮件模板

    jenkins 发送邮件模板 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  7. springboot集成模板引擎freemarker和thymeleaf

    freemarkder和thymeleaf都是java的模板引擎,这里只介绍这两种模板引擎如何在sprongboot中配置: 1. freemarkder 1.1 在pom.xml中添加依赖包 < ...

  8. springboot之模板

    转:http://jisonami.iteye.com/blog/2301387,http://412887952-qq-com.iteye.com/blog/2292402 整体步骤:(1)     ...

  9. 记录一次简单的springboot发送邮件功能

    场景:经常在我们系统中有通过邮件功能找回密码,或者发送生日祝福等功能,今天记录下springboot发送邮件的简单功能 1.引入maven <!-- 邮件开发--><dependen ...

随机推荐

  1. BootStrap DateTimePicker的使用

    使用方法 1. 添加资源 2. 编写代码 <!DOCTYPE html> <html> <head> <meta http-equiv="Conte ...

  2. Stanford机器学习---第十一讲.异常检测

    之前一直在看Standford公开课machine learning中Andrew老师的视频讲解https://class.coursera.org/ml/class/index 同时配合csdn知名 ...

  3. 谈谈程序猿求职简历的STAR法则

    最近Android/IOS的市场都有点躁动,前两天看业内几位大牛写的关于求职招聘的文章,觉得说的很有道理,暂且吸收进来,同时讲一下简历面试中使用STAR法则.我们在写简历或者面试时,最主要的一个目的, ...

  4. Shell脚本应用(for、while循环语句和case分支语句)

    1.for:读取不同的变量值,逐个执行同一组命令,直到取值完毕退出,变量值以空格分隔 语法: for  变量值  in  取值列表 do 命令序列 done 2.while:重复测试某个条件,成立则执 ...

  5. Linux 内存使用率

    文章参考: 1.正确计算linux系统内存使用率 2.Linux系统内存消失与slab使用之谜 例如当前主机内存信息如下: [zhang@test ~]$ cat /proc/meminfo MemT ...

  6. 启动Myeclipse报错“Failed to create the Java Virtual Machine”的解决办法

    我安装的是Myeclipse 10.7.1.装上好久没用,今天启动突然报错:Failed to create the Java Virtual Machine. 检查Myeclipse安装好使用时好的 ...

  7. 计算器和Menu

    MainActivity.java import android.app.Activity; import android.content.Intent; import android.os.Bund ...

  8. Arduino IDE for ESP8266教程(三)HTTP客户端

    整个教程就以上传Yeelink数据点为目的制作一个HTTP客户端. 1获取账号 http://www.cnblogs.com/imfanqi/p/4419915.html 2代码教程 http://w ...

  9. 以太坊中的Ghost协议

    https://blog.csdn.net/t46414704152abc/article/details/81191804 写得超好,终于弄懂了什么是叔块,怎么确定哪条链最长,以太坊与比特币出块的差 ...

  10. day22 Pythonpython 本文json模块

    json模块 •应用场景: json模块主要用于处理json格式的数据,可以将json格式的数据转化为python的字典,便于python处理,同时也可以将python的字典或列表等对象转化为json ...