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. Kotlin入门(12)类的概貌与构造

    上一篇文章提到泛型函数appendString是在类外面定义,这不免使人疑惑,类里面又该怎样定义成员函数呢?为解答这个疑问,接下来的几篇文章将好好描述一下Kotlin如何操作类及其对象,本篇文章先对类 ...

  2. Spring security实现国际化问题

    这两天Spring用户登录国际化这个问题困扰我好久啊,于昨天晚上终于把它干掉了. 场景就是我们公司的产品-incopat,需要支持中英文,用户登录这块用的spring自带的security,需求讲的通 ...

  3. 第六章 Hyper-V 2012 R2 的检查点

    "检查点"是 Windows Server 2012 R2 中对 Windows Server 2012 及以前版本的 Hyper-V"快照"功能的新称呼.之所 ...

  4. 转 让Python在Android系统上飞一会儿

    让Python在Android系统上飞一会儿 地址: http://blog.csdn.net/ccwwff/article/details/6208260

  5. WampServer 安装使用详解

    WampServer集成环境的搭建.安装.使用.配置 什么是WampServer WampServer是一款由法国人开发的Apache Web服务器.PHP解释器以及MySQL数据库的整合软件包.免去 ...

  6. php开发中遇到的一些问题

    php警告提示A session had already been started – ignoring session_start() 解决方案 判断 如果session_id 不存在,说明没有储存 ...

  7. nginx基础知识总结

    1.nginx的工作模式 master/worker工作模式: 一个master进程: 负载加载和分析配置文件.管理worker进程.平滑重启升级等. 一个或多个worker进程 处理并响应用户请求 ...

  8. python redirect和render的区别

    render是渲染变量到模板中,而redirect是HTTP中1个跳转的函数,一般会生成302状态码.

  9. css设置标签居中

    position: absolute;  //相对于已经定位的父元素的位置. left: 50%; top: 50%; transform: translate(-50%,50%);

  10. 采用spring的schedule注解配置定时任务

    1 在springmvc配置文件中新增以下配置 <!-- 此处对于定时时间的配置会被注解中的时间配置覆盖,因此,以注解配置为准 --> <task:scheduled-tasks s ...