Java Mail+Thymeleaf模板引擎实现发送HTML格式邮件
Java Mail+Thymeleaf模板引擎实现发送HTML格式邮件
基于Spring boot 1.5,Spring boot 2.x请使用Spring boot mail
1.依赖坐标
// buildscript 代码块中脚本优先执行
buildscript {//springBootVersion = '1.5.2.RELEASE' 经典稳定版本// ext 用于定义动态属性ext {springBootVersion = '1.5.2.RELEASE'}// 自定义 Thymeleaf 和 Thymeleaf Layout Dialect 的版本ext['thymeleaf.version'] = '3.0.3.RELEASE'ext['thymeleaf-layout-dialect.version'] = '2.2.0'// 自定义 Hibernate 的版本ext['hibernate.version'] = '5.2.8.Final'// 使用了 Maven 的中央仓库(你也可以指定其他仓库)repositories {//mavenCentral()maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'}}// 依赖关系dependencies {// classpath 声明说明了在执行其余的脚本时,ClassLoader 可以使用这些依赖项classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")}
}// 使用插件
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'// 打包的类型为 jar,并指定了生成的打包的文件名称和版本
jar {baseName = 'blog-search'version = '1.0.0'
}// 指定编译 .java 文件的 JDK 版本
sourceCompatibility = 1.8// 默认使用了 Maven 的中央仓库。这里改用自定义的镜像库
repositories {//mavenCentral()maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'}
}// 依赖关系
dependencies {// 该依赖对于编译发行是必须的compile('org.springframework.boot:spring-boot-starter-web')// 添加 Thymeleaf 的依赖compile('org.springframework.boot:spring-boot-starter-thymeleaf')// 添加 Spring Security 依赖compile('org.springframework.boot:spring-boot-starter-security')// 添加 Spring Boot 开发工具依赖//compile("org.springframework.boot:spring-boot-devtools")// 添加 Spring Data JPA 的依赖compile('org.springframework.boot:spring-boot-starter-data-jpa')// 添加 MySQL连接驱动 的依赖compile('mysql:mysql-connector-java:6.0.5')// 添加 Thymeleaf Spring Security 依赖,与 Thymeleaf 版本一致都是 3.xcompile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4:3.0.2.RELEASE')// 添加 Apache Commons Lang 依赖compile('org.apache.commons:commons-lang3:3.5')// 添加 Markdown parser 依赖compile('es.nitaur.markdown:txtmark:0.16')// 添加 Spring Data Elasticsearch 的依赖compile('org.springframework.boot:spring-boot-starter-data-elasticsearch')// 添加 JNA 的依赖compile('net.java.dev.jna:jna:4.3.0')// 该依赖对于编译测试是必须的,默认包含编译产品依赖和编译时依testCompile('org.springframework.boot:spring-boot-starter-test')/*FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页、电子邮件、配置文件、源代码等)的通用工具。它不是面向最终用户的,而是一个Java类库,是一款可以嵌入所开发产品的组件。*///compile('org.springframework.boot:spring-boot-starter-freemarker')//添加 JavaMail 发送邮件compile("javax.mail:javax.mail-api:1.5.6")compile("com.sun.mail:javax.mail:1.5.6")//添加Spring Boot Mail 的依赖,注意SpringBoot2.x以上版本//compile("org.springframework.boot:spring-boot-starter-mail")
}
2.配置文件
#Java Mail
# 邮件服务器地址
mail.host=smtp.qq.com
#发件人邮箱地址
mail.user=***@qq.com
#发件人邮箱密码
mail.password=****
3.邮件工具类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Map;
import java.util.Properties;@Component
public class MailTemplateSenderUtils {@Value("${mail.user}")private String USER;@Value("${mail.password}")private String PASSWORD;@Value("${mail.host}")private String HOST;@Autowiredprivate TemplateEngine templateEngine;public Boolean sendMail(Map<String, Object> valueMap) {try {final Properties props = new Properties();props.put("mail.smtp.auth", "true");props.put("mail.smtp.host", HOST);// 发件人的账号props.put("mail.user", USER);//发件人的密码props.put("mail.password", PASSWORD);// 构建授权信息,用于进行SMTP进行身份验证Authenticator authenticator = new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {// 用户名、密码String userName = props.getProperty("mail.user");String password = props.getProperty("mail.password");return new PasswordAuthentication(userName, password);}};// 使用环境属性和授权信息,创建邮件会话Session mailSession = Session.getInstance(props, authenticator);// 创建邮件消息MimeMessage message = new MimeMessage(mailSession);// 设置发件人String username = props.getProperty("mail.user");InternetAddress form = new InternetAddress(username);message.setFrom(form);// 设置收件人String to = (String) valueMap.get("to");InternetAddress toAddress = new InternetAddress(to);message.setRecipient(Message.RecipientType.TO, toAddress);// 设置邮件标题String title = (String) valueMap.get("title");message.setSubject(title);// 设置邮件的内容体Context context = new Context();context.setVariables(valueMap);String content = templateEngine.process("MailTemplate", context);message.setContent(content, "text/html;charset=UTF-8");// 发送邮件Transport.send(message);return true;} catch (Exception e) {e.printStackTrace();}return false;}
}
Java Mail+Thymeleaf模板引擎实现发送HTML格式邮件相关推荐
- SpringBoot框架+Thymeleaf模板引擎实现发送HTML格式邮件(可带附件)
spring-boot-mail 项目结构 1.Maven工程依赖坐标 注意:SpringBoot版本需为2.x 若spring boot版本为1.x, <?xml version=" ...
- java 模板引擎_SpringBoot入门系列(四)如何整合Thymeleaf模板引擎
前面介绍了Spring Boot的优点,然后介绍了如何快速创建Spring Boot 项目.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/ ...
- java 模板引擎_Spring Boot 如何快熟整合Thymeleaf模板引擎
前面介绍了Spring Boot的优点,然后介绍了如何快速创建Spring Boot 项目.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/ ...
- 发送邮件功能:使用Spring Email、邮件工具类、使用Thymeleaf模板引擎 发送html邮件
发送邮件 Spring Email 开启自己邮箱的POP3/SMTP服务 导入spring mail 依赖 <!-- https://mvnrepository.com/artifact/org ...
- 【SpringBoot】3、SpringBoot中整合Thymeleaf模板引擎
SpringBoot 为我们提供了 Thymeleaf 自动化配置解决方案,所以我们在 SpringBoot 中使用 Thymeleaf 非常方便 一.简介 Thymeleaf是一个流行的模板引擎,该 ...
- 玩转springboot:thymeleaf模板引擎入门程序
一.前言 常用的模板引擎有:JSP.Velocity.Freemarker.Thymeleaf 但是,Springboot默认是不支持JSP的,默认使用thymeleaf模板引擎.而且,语法更简单,功 ...
- 九、SpringBoot集成Thymeleaf模板引擎
Thymeleaf咋读!??? 呵呵,是不是一脸懵逼...哥用我的大学四级英文知识告诉你吧:[θaimlif]. 啥玩意?不会音标?...那你就这样叫它吧:"赛母李府",大部分中国 ...
- thymeleaf模板引擎
文章目录 前言 一.thymeleaf是什么? 二.使用步骤 1.导入坐标 2.Spring Boot项目中创建controller 3.分析源码 4.配置success.html 5.启动主配置文件 ...
- Thymeleaf模板引擎---SpringBoot
Thymeleaf模板引擎 前端交给我们的页面,是html页面.如果是我们以前开发,我们需要把他们转成jsp页面,jsp好处就是当我们查出一些数据转发到JSP页面以后,我们可以用jsp轻松实现数据的显 ...
最新文章
- 模仿android谷歌地图功能开发记录
- 怎么设计计算机网络共享,如何设置网络共享 网络共享设置方法【详解】
- 腾讯张正友:计算机视觉的三生三世
- vim 的substitute
- ibatisnet 学习手记(1)
- 推荐 12个Idea 免费实用插件及插件离线安装方法
- x61 linux 驱动 无线网卡,Linux环境Thinkpad X61 4G内存Mtrr表错误
- Netty工作笔记0064---WebSocket长连接开发3
- Python Day17 Django 03
- Android 轻松实现语音识别详解及实例代码
- javaweb使用 window.location.href 传中文参数 乱码问题
- MySQL基础(DML,DQL,增删改查)
- python实现AES加密解密
- 每年都要调两次时间,美国人已经烦透了
- Native开发工具之静态库和动态库(二,小码农也有大梦想
- Windows下CMD操作常用指令详解
- 颜色迁移之四——模糊聚类(FCM)算法
- 计算机网络近年的发展趋势,【网络技术论文】计算机网络技术发展趋势探析(共2622字)...
- GEA 1.6 运行时引擎架构
- react合并单元格
热门文章
- C程序员眼里的Python
- js 一/二维数组排序
- CodeSign error: code signing is required for product type Application in SDK iOS
- 管道半双工通信程序linux,Linux进程间通信的几种方法-半双工管道,命名管道,消息队列...
- java 函数名调用_粉丝提问|c语言:如何定义一个和库函数名一样的函数,并在函数中调用该库函数...
- mysql错误1300怎么解决_pt-osc 变更时遇到 “MySQL error 1300” 报错问题解决
- vue dplayer 加载失败_最新vue脚手架项目搭建,并解决一些折腾人的问题
- python训练营微信公众号真实性_用python进行微信公众号开发(仅测试学习)
- 基于物化视图优化_CVPR2017|基于构造多视图子空间中的潜在表示解决聚类问题
- html点击子元素隐藏div,jq 点击div,显示当前div内容,隐藏其他div的子元素