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 ...
随机推荐
- ViewPager+Fragment切换卡顿解决办法
1.ViewPager设置预加载 我有4个tag,都不想被销毁,设置预加载个数为3. ViewPager viewPager; viewPager.setOffscreenPageLimit(3); ...
- Python笔记(十四):操作excel openpyxl模块
(一) 常遇到的情况 就我自己来说,常遇到的情况可能就下面几种: 读取excel整个sheet页的数据. 读取指定行.列的数据 往一个空白的excel文档写数据 往一个已经有数据的excel文档追加 ...
- python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie)
python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie) 主要包括两部分内容:(1)利用python中的dict实现Trie:(2) ...
- Spark编译
Spark的运行版本使用mvn编译,已经集成在源码中.如果机器有外网或者配置了http代理,可以直接调用编译命令来进行编译. windows&Linux命令如下: ./build/mvn \ ...
- [20171227]表的FULL_HASH_VALUE值的计算.txt
[20171227]表的FULL_HASH_VALUE值的计算.txt --//sql_id的计算是使用MD5算法进行哈希,生成一个128位的Hash Value,其中低32位作为HASH VALUE ...
- python自动化报告的输出
1.设计简单的用例 2.设计用例 以TestBaiduLinks.py命名 # coding:utf-8 from selenium import webdriver import unittest ...
- python第九十六天 ---Django(1)
django 模块 一 安装: pip3 install django 或 python -m pip install django 二 添加环境变量 相关命令: #cmd 下 django-ad ...
- bootstrap-select插件 多选框
HeBeiTianQi.jsp页面 1 <!--bootstrap-select .css引用--> 2 <link rel="stylesheet" href= ...
- macOS 下NFS 文件系统挂载
主要有两种方式: 使用:resvport选项, mount 挂载命令时. 使用:insecure选项, exportfs 文件配置时. sudo mount -o resvport IP:Addr b ...
- 【PAT】B1068 万绿丛中一点红(20 分)
一开始因为看见这题就头疼,可费了点时间. 要考虑所有元素,如果忽略外圈元素,最后一个样例过不去. 而且要求只出现一次的元素, 我没有使用map,因为当时还没学,用map储存元素会节约好多代码 #inc ...