springboot 发送邮件+模板+附件
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 发送邮件+模板+附件的更多相关文章
- SpringBoot 发送邮件和附件
作者:yizhiwaz 链接:www.jianshu.com/p/5eb000544dd7 源码:https://github.com/yizhiwazi/springboot-socks 其他文章: ...
- 1.使用javax.mail, spring的JavaMailSender,springboot发送邮件
一.java发邮件 电子邮件服务器:这些邮件服务器就类似于邮局,它主要负责接收用户投递过来的邮件,并把邮件投递到邮件接收者的电子邮箱中,按功能划分有两种类型 SMTP邮件服务器:用户替用户发送邮件和接 ...
- SpringBoot thymeleaf模板页面没提示,SpringBoot thymeleaf模板插件安装
SpringBoot thymeleaf模板插件安装 SpringBoot thymeleaf模板Html页面没提示 SpringBoot thymeleaf模板页面没提示 SpringBoot t ...
- SpringBoot 发送邮件功能实现
背景 有个小伙伴问我你以前发邮件功能怎么弄的.然后我就给他找了个demo,正好在此也写一下,分享给大家. 理清痛点 发送邮件,大家可以想一下,坑的地方在哪? 我觉得是三个吧. 第一:邮件白名单问题. ...
- SpringBoot thymeleaf模板版本,thymeleaf模板更换版本
SpringBoot thymeleaf模板版本 thymeleaf模板更换版本 修改thymeleaf模板版本 ================================ ©Copyright ...
- jenkins 发送邮件模板
jenkins 发送邮件模板 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- springboot集成模板引擎freemarker和thymeleaf
freemarkder和thymeleaf都是java的模板引擎,这里只介绍这两种模板引擎如何在sprongboot中配置: 1. freemarkder 1.1 在pom.xml中添加依赖包 < ...
- springboot之模板
转:http://jisonami.iteye.com/blog/2301387,http://412887952-qq-com.iteye.com/blog/2292402 整体步骤:(1) ...
- 记录一次简单的springboot发送邮件功能
场景:经常在我们系统中有通过邮件功能找回密码,或者发送生日祝福等功能,今天记录下springboot发送邮件的简单功能 1.引入maven <!-- 邮件开发--><dependen ...
随机推荐
- 1.Spring MVC详解
目录 1.SpringMVC 详细介绍 2.SpringMVC 处理请求流程 3.配置前端控制器 4.配置处理器适配器 5.编写 Handler 6.配置处理器映射器 7.配置视图解析器 8.Disp ...
- [20180604]在内存修改数据(bbed).txt
[20180604]在内存修改数据(bbed).txt --//以前曾经做过在内存修改数据,通过oradebug poke命令修改内存信息,相关链接:--//http://blog.itpub.net ...
- 【第二篇】SAP ABAP7.5x新语法之OPEN SQL
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:SAP ABAP7.5x系列之OPEN SQL ...
- sql语句进阶教程
转载自:http://blog.csdn.net/u011001084/article/details/51318434 最近从图书馆借了本介绍SQL的书,打算复习一下基本语法,记录一下笔记,整理一下 ...
- rpm安装时出现循环依赖
在安装git包时提示要安装perl-git,当安装perl-git时又提示要安装git包.报错如下: [root@racdb1 Packages]# rpm -ivh perl-Git-1.7.1-4 ...
- C++基础算法学习——熄灯问题
有一个由按钮组成的矩阵, 其中每行有6个按钮, 共5行– 每个按钮的位置上有一盏灯– 当按下一个按钮后, 该按钮以及周围位置(上边, 下边,左边, 右边)的灯都会改变状态26熄灯问题 POJ1222– ...
- Windows Server 2012 RS 配置IIS8.0+发布网站
一.配置iis 8.0 IIS 8.0 是 windows server 2012 自带的服务器管理系统.相比之前版本,IIS 8.0 安装和操作都更加简单,界面也很简洁,安装也很迅速. 1. 进入w ...
- oracle用户权限和角色
用户权限是最基本的数据库安全控制.其作用就是控制用户对数据的访问,sql语句的执行.当oracle用户创建的时候,可以通过授权来控制用户对数据的连接,数据库对象的操作等等. 在oracle中,分为两种 ...
- indexOf() 使用方法(数组去重)
对于indexOf()的用法一直停留在查找第几个字符串,却不知道它能用到数组去重中,首先还是温顾下indexOf()的语法: <!DOCTYPE html> <html lang=& ...
- oracle的order by排序中空字符串处理方法
1.缺省处理 Oracle在Order by 时缺省认为null是最大值,所以如果是ASC升序则排在最后,DESC降序则排在最前 2.使用nvl函数 nvl函数可以将输入参数为空时转换为一特定值,如 ...