Spring邮件发送2
前言:上一篇博文讲解了邮件发送的基础用法(数据是写死的),然而在实际开发中,大多数情况下邮件内容都是根据业务来动态生成的。所以在此篇博文中,我们将讲解邮件发送携带数据的几种方案。
一、解析自定义占位符
实现方法: 通过解析自定义占位符,将传递到邮件中的数据,转换成html内容,进行发送。
1)占位符替换函数
/**
* Replaces place holder ("${}") string in template with values in Map
*
* @param template
* @param models
* @return
*/
public static String replacePlaceHolder(String template, Map<String, String> models) { if (template.indexOf("${") == -1) {
return template;
} while (true) { int start = template.indexOf("${");
int end = template.indexOf("}", start); if (start != -1 && end != -1) { String key = template.substring(start + 2, end); if (models.containsKey(key)) {
template = template.substring(0, start) + models.get(key) + template.substring(end + 1);
} } else {
break;
} } return template; }
2) 邮件发送Test-Case
// 1. Resolve html template to real text
String htmlTemplate = "<html lang='zh-cn'><head></head><body><h1>发送带模板数据的Email</h1><p>你好,${username}。本次您的验证码为${code},请妥善保管</p></body></html>"; Map<String, String> models = new HashMap<String, String>();
models.put("username", "XXX");
models.put("code", "4551"); String text = StringUtils.replacePlaceHolder(htmlTemplate, models); // 2. send email
boolean result = mailHandler.sendText("收件人邮箱", "发送带模板数据的Email", text);
Assert.assertEquals(true, result);
二、使用Velocity模板
实现方法:借用VelocityEngineUtils合并Velocity模板和数据,得到要发送的Email, 进行发送。
1) 引入依赖jar包
<!-- velocity -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
2) 配置Velocity模板引擎
<!-- Define velocity engine -->
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="configLocation" value="classpath:velocity.properties" />
</bean>
velocity.properties:
input.encoding=UTF-8
output.encoding=UTF-8
resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
3) 发送邮件实现函数
// Get email content by velocity merge models
String content = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, template, encoding, models); // Send email
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, encoding);
helper.setFrom(form); // set sender
helper.setTo(to); // set recipients
helper.setSubject(subject);
helper.setText(content, true); // Indicate the text included is HTML
mailSender.send(mimeMessage);
三、使用FreeMarker模板
实现方法:借用FreeMarkerTemplateUtils合并FreeMarker模板和数据,得到要发送的Email, 进行发送。
1) 引入依赖jar包
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
2)配置FreeMarker
<!-- Define freemarker configuration -->
<bean id="freeMarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
<property name="templateLoaderPath" value="classpath:template/freemarker" />
<property name="defaultEncoding" value="utf-8" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">10</prop>
<prop key="locale">zh_CN</prop>
<prop key="number_format">#.##</prop>
</props>
</property>
</bean>
3) 发送邮件实现函数
// Get email content by freeMarker template
Template realTemplate = freeMarkerConfigurer.getTemplate(template);
String content = FreeMarkerTemplateUtils.processTemplateIntoString(realTemplate, models);
// Send email
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, encoding);
helper.setFrom(form); // set sender
helper.setTo(to); // set recipients
helper.setSubject(subject);
helper.setText(content, true); // Indicate the text included is HTML
mailSender.send(mimeMessage);
Spring邮件发送2的更多相关文章
- Spring 邮件发送
前言:以前都是直接用Java自带的邮件工具发送邮件,现在Spring帮我们做了封装,提供了更好用更简单的发送邮件工具JavaMailSender 关于邮件服务器的设置就不在这里说了,直接去QQ邮箱 ...
- java spring 邮件发送
开发中经常会遇到发送邮件进行用户验证,或者其它推送信息的情况,本文基于spring,完成邮件的发送,主要支持普通文本邮件的发送,html文本邮件的发送,带附件的邮件发送,没有实现群发.多个附件发送等需 ...
- Spring邮件发送1
注意:邮件发送code中,邮件服务器的申请和配置是比较主要的一个环节,博主这里用的是QQ的邮件服务器.有需要的可以谷歌.百度查下如何开通. 今天看了下Spring的官方文档的邮件发送这一章节.在这里记 ...
- spring邮件发送
1,Emaill类: package com.learn.jsp.pojo; /** * 邮件基本信息 * @author kevin * */public class Email { private ...
- Spring的javaMail邮件发送(带附件)
项目中经常用到邮件功能,在这里简单的做一下笔记,方便日后温习. 首先需要在配置文件jdbc.properties添加: #------------ Mail ------------ mail.smt ...
- 使用spring的邮件发送功能
使用spring提供的MailSender和JavaMailSender类. 1.邮件对象类 package cn.luxh.app.mail; import java.util.List; impo ...
- 使用Spring的JAVA Mail支持简化邮件发送(转)
闲来无事,翻看<Spring in Action>,发现Spring集成了对JAVA Mail的支持,有点小激动的看了一遍,嗯,话说真的简单了很多. Spring的邮件发送的核心是Mail ...
- Spring Boot 2.0 图文教程 | 集成邮件发送功能
文章首发自个人微信公众号: 小哈学Java 个人网站: https://www.exception.site/springboot/spring-boots-send-mail 大家好,后续会间断地奉 ...
- Spring Boot整合邮件发送
概述 Spring Boot下面整合了邮件服务器,使用Spring Boot能够轻松实现邮件发送:整理下最近使用Spring Boot发送邮件和注意事项: Maven包依赖 <dependenc ...
随机推荐
- JVM垃圾收集器&对象的引用回收
1.介绍垃圾收集器 垃圾收集器(Garbage Collection,GC)就是用于回收方法区和堆区,其他程序计数器.虚拟机栈.本地方法栈这3个区域都是随线程而生,随线程而灭,栈中的栈帧会随着方法的进 ...
- Redis 安装简介
Redis 是一个高性能的key-value数据库. redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部 分场合可以对关系数据库起到很好的补充作用.它提供了Jav ...
- Unity 读取资源(图片)
方法一: 采用Resource.Load方法读取,读取在Unity中Assets下Resources目录下的资源名(不采用后缀). //图片放在Asset/Resources/ Texture2D t ...
- JAVA获取文件数据 ( xxxxx.json )
//路径fPixFile filePath = new File(fPix);System.out.print("文件路径:" + filePath);try { if (file ...
- Mac下使用终端连接远程使用ssh协议的git服务器
最近换了台新电脑, MacBook pro,拿到新电脑之后小小心喜了一下(终于解脱windows的束缚拥抱mac啦), 然后就开始苦逼的安装各种开发环境了. 之前在windows上使用tortoise ...
- eclipse报错排解
一.解决eclipse中git插件中的cannot open git-upload-pack问题 有时候在eclipse上使用插件egit向github或者osc上同步代码时,有时候会发现出现cann ...
- 笔记:Maven 生命周期与命令行详解
Maven 拥有三套相互独立的生命周期,分别是 clean.default和site,clean 生命周期的目的是清理项目,default 生命周期的目的是构建项目,而site生命周期的目的是建立项目 ...
- Vue常用开源项目汇总
前言:Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层,不仅易于上手,还 ...
- 【Docker】 Swarm简单介绍
[Swarm] Swarm是Docker官方提供的一款集群管理工具,其主要作用是把若干台Docker主机抽象为一个整体,并且通过一个入口统一管理这些Docker主机上的各种Docker资源.Swarm ...
- CSS 剩余宽度和高度完全填充
<html><head><meta http-equiv="Content-Type" content="text/html; charse ...