1、邮件模板数据库结构设计:
DROP TABLE IF EXISTS `email`;
CREATE TABLE `email` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`paramter` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`context` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of email
-- ----------------------------
INSERT INTO `email` VALUES (1, 'name,money,point', '您好${name},晚上好!您目前余额:${money?string(r\"#.##r\")}元,积分:${point}', 'emailtemplate');
2、SpringBoot 模板封装结构图:
3、核心代码:
pom.xml 文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zzg</groupId>
<artifactId>mysql-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>mysql-boot-template</artifactId>
<dependencies>
<!--freemarker模板引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
</project>
java代码:
package com.zzg.freemarker.util;
import java.io.StringWriter;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
/**
*
* @ClassName: FreeMarkerUtil
* @Description: freemarker 工具类
* @author: **** -zzg
* @date: 2019年4月10日 下午2:17:56
*
* @Copyright: 2019 www.digipower.cn
* 注意:本内容仅限于****开发有限公司内部使用,禁止用于其他的商业目的
*/
public class FreeMarkerUtil {
// 引入日志
private final static Logger logger = LoggerFactory.getLogger(FreeMarkerUtil.class);
public static String transform(Map map, String context){
try {
//定义StringTemplateLoader
StringTemplateLoader loader = new StringTemplateLoader();
loader.putTemplate("template", context);
//定义Configuration
Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
configuration.setTemplateLoader(loader);
//定义Template
Template tpl = configuration.getTemplate("template");
StringWriter writer = new StringWriter();
tpl.process(map, writer);
return writer.toString();
} catch (Exception e) {
logger.error(e.getMessage());
return null;
}
}
}
4、模板邮件功能测试:
@RequestMapping(value="/sendTemplate", method=RequestMethod.GET)
@ResponseBody
public void sendTemplate(){
List<Email> list = service.getEmails();
if(list != null && list.size() > 0){
list.stream().forEach(item ->{
// 简单文本邮件发送
EmailBean text = new EmailBean();
text.setFrom("zhouzhiwengang@163.com");
text.setTo("zhouzhigang@digipower.cn");
text.setSubject("邮件模板发送");
Map<String,Object> parameter = new HashMap<String,Object>();
parameter.put("name", "周志刚");
parameter.put("money", 1000089.155);
parameter.put("point", 16568);
try {
// 声明模板和模板参数写入(核心代码)
String context = FreeMarkerUtil.transform(parameter, item.getContext());
if(StringUtils.isEmpty(context)){
// 判断freemarker 模板解析是否正确解析
logger.error("freemarker 模板解析错误");
return;
}
text.setText(context);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
text.setType(Integer.valueOf(1));
util.send(text);
logger.error("HTML邮件发送成功");
});
}