`
kanpiaoxue
  • 浏览: 1744614 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Spring 使用Freemarker发送邮件的一个简单模板(代码不完整)

 
阅读更多

下面是一个使用过的Freemarker的模板,可以作为参考(代码不完整)。附件就是它的源文件。

dmap-alarm-template-mail.ftl

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>报警邮件</title>
<style>
*{
	font-size: 12px;
}
.mailTitle{
	text-align: center;
	margin-top: 25px;
}
.mailBody{
	margin: 25 25 25 25;
}
.mailTitleTaskName{
	font-size: 16px;
	color: red;
	font-weight: bolder;
}
.mailTitleErrorMessage{
	font-size: 16px;
	color: red;
	font-weight: bolder;	
}
table{margin-left:25px; margin-top:10px; width:95%; border:1px blue solid;}
td{ border:1px solid #cccccc;padding:5px 5px 5px 5px;}
caption{
	background-color: rgb(95,175,17);
	font-weight: bold;
	padding-top: 5px;
	padding-bottom:5px;
}

.copyRight{text-align: center;font-size: 14px; font-weight:bold;color: silver;margin-top:10px;margin-bottom:25px;}
.firstRow{text-align: center;font-size: 14px;font-weight:bold;}
.run_other_state{color: blue;font-weight:bold;}
.run_wait_running{color: silver;font-weight:bold;}
.run_success{color: green;font-weight:bold;}
.run_failure{color: red;font-weight:bold;}
.current_first_column{width:30%;font-weight:bold;}
</style>
</head>
	<body>
		<div class="mailTitle">
			<div class="mailTitleTaskName">任务ID:${current_task_id}</div>
			<div>数据版本:${current_dataversion_no}</div>
			<div class="mailTitleErrorMessage">${current_task_error_message}</div>
		</div>
		<div class="mailBody">
			<table>
				<caption>任务信息</caption>
				<tr>
					<td class="current_first_column">任务ID</td>
					<td>${current_task_id}</td>
				</tr>
				<tr>
					<td class="current_first_column">任务名称</td>
					<td>${current_task_name}</td>
				</tr>
				<tr>
					<td class="current_first_column">数据版本最后更新时间</td>
					<td>${current_dataversion_last_mofied}</td>
				</tr>
				<tr>
					<td class="current_first_column">所属团队</td>
					<td>${current_task_team_name}</td>
				</tr>
				<tr>
					<td class="current_first_column">任务启动延迟时间点</td>
					<td>${current_launched_timeout}</td>
				</tr>
				<tr>
					<td class="current_first_column">运行超时时间</td>
					<td>${current_running_timeout}分钟</td>
				</tr>
				<tr>
					<td class="current_first_column">版本运行信息</td>
					<td>
					<textarea rows="5" style="width:100%;text-align:left;">${current_data_version_message!""}</textarea>					
					</td>
				</tr>
				<tr>
					<td class="current_first_column">第一值班人</td>
					<#if current_task_duty_first??>
						<td>${current_task_duty_first.name}(${current_task_duty_first.username},手机号码:${(current_task_duty_first.mobileNumber)!"无"},邮箱:<a href="mailto:${current_task_duty_first.email}">${current_task_duty_first.email}</a>)</td>
					<#else>
						<td>无</td>
					</#if>
				</tr>
				<tr>
					<td class="current_first_column">第二值班人</td>
					<#if current_task_duty_second??>
						<td>${current_task_duty_second.name}(${current_task_duty_second.username},手机号码:${(current_task_duty_first.mobileNumber)!"无"},邮箱:<a href="mailto:${current_task_duty_second.email}">${current_task_duty_second.email}</a>)</td>
					<#else>
						<td>无</td>
					</#if>
				</tr>
				<tr>
					<td class="current_first_column">直接下游任务数量(DOWN)</td>
					<td>${current_task_down_stream_count}</td>
				</tr>
			</table>

			<#if down_stream_effect_map?exists>
			<#assign rowIndex = 0>
				<#assign mapsize = down_stream_effect_map?size>
				<#if (mapsize>0)>	
				<table style="text-align: center;">
					<caption>当前任务的直接下游任务列表</caption>
					<tr class="firstRow">
						<td>#</td>
						<td>任务类型</td>
						<td>任务ID</td>
						<td>任务名称</td>
						<td>数据版本</td>
						<td>所属团队</td>
						<td>第一值班人</td>
						<td>第二值班人</td>
					</tr>
						<#list down_stream_effect_map?keys as key>
							<#assign ls = down_stream_effect_map[key]>
							<#assign rowspan = ls?size>
							<#list ls as object>
								<#assign rowIndex = rowIndex + 1>
								<tr>
								<td>${rowIndex}</td>
								<#if object_index == 0>
									<td rowspan="${rowspan}">下游任务</td>
									<td rowspan="${rowspan}">${object.taskIdString}</td>
									<td rowspan="${rowspan}">${object.taskName}</td>
								</#if>
								<td>${object.dataVersionNo}</td>
								<#if object_index == 0>
									<td rowspan="${rowspan}">${object.teamName}</td>
									<#if object.firstUser??>
										<td rowspan="${rowspan}">${object.firstUser.name}(${object.firstUser.username},手机号码:${(object.firstUser.mobileNumber)!"无"},邮箱:<a href="mailto:${object.firstUser.email}">${object.firstUser.email}</a>)</td>
									<#else>
										<td rowspan="${rowspan}">无</td>
									</#if>
									<#if object.secondUser??>
										<td rowspan="${rowspan}">${object.secondUser.name}(${object.secondUser.username},手机号码:${(object.secondUser.mobileNumber)!"无"},邮箱:<a href="mailto:${object.secondUser.email}">${object.secondUser.email}</a>)</td>
									<#else>
										<td rowspan="${rowspan}">无</td>
									</#if>
								</#if>
							</tr>																					
							</#list>
						</#list>					
				</table>
			</#if>
			</#if>
			<div class="copyRight">@Copyright: 运营产品研发部-CDC[2014.11-${current_year_month}]</div>
		</div>
	</body>
</html>

 

import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.template.Template;

import java.util.Map;

public class TemplateEmailService {
    private FreeMarkerConfigurer freeMarkerConfigurer;
    private String fileName;
    public FreeMarkerConfigurer getFreeMarkerConfigurer() {
        return freeMarkerConfigurer;
    }

    // 通过模板构造邮件内容,参数username将替换模板文件中的${username}标签。
    public <K, V> String getMailText(Map<K, V> map) {
        String htmlText = "";
        try {
            // 通过指定模板名获取FreeMarker模板实例
            Template tpl = freeMarkerConfigurer.getConfiguration().getTemplate(
                    fileName);
            // FreeMarker通过Map传递动态数据
            // 解析模板并替换动态数据,最终username将替换模板文件中的${username}标签。
            htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(tpl,
                    map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return htmlText;
    }

    public void setFreeMarkerConfigurer(
            FreeMarkerConfigurer freeMarkerConfigurer) {
        this.freeMarkerConfigurer = freeMarkerConfigurer;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    
}

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:sec="http://www.springframework.org/schema/security"
	xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"

	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
		http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.0.xsd
		http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

	<bean id="freeMarker"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPath" value="classpath:org/kanpiaoxue/test/freemarker" /><!--指定模板文件目录 -->
		<property name="freemarkerSettings"><!-- 设置FreeMarker环境属性 -->
			<props>
				<prop key="template_update_delay">1800</prop><!--刷新模板的周期,单位为秒 -->
				<prop key="default_encoding">UTF-8</prop><!--模板的编码格式 -->
				<prop key="locale">zh_CN</prop><!-- 本地化设置 -->
			</props>
		</property>
	</bean>
	<bean id="templateEmail" class="org.kanpiaoxue.test.freemarker.TemplateEmailService">
		<property name="freeMarkerConfigurer" ref="freeMarker"></property>
	</bean>
</beans>

 main 函数的部分代码:

    private static final String CURRENT_TASK_ID = "current_task_id";
    private static final String CURRENT_TASK_NAME = "current_task_name";
    private static final String CURRENT_TASK_TEAM_NAME = "current_task_team_name";
    private static final String CURRENT_TASK_ERROR_MESSAGE = "current_task_error_message";
    private static final String CURRENT_TASK_DUTY_FIRST = "current_task_duty_first";
    private static final String CURRENT_TASK_DUTY_SECOND = "current_task_duty_second";
    private static final String CURRENT_TASK_DOWN_STREAM_COUNT = "current_task_down_stream_count";
    private static final String CURRENT_DATAVERSION_NO = "current_dataversion_no";
    private static final String CURRENT_DATAVERSION_LAST_MOFIED = "current_dataversion_last_mofied";
    private static final String CURRENT_LAUNCHED_TIMEOUT = "current_launched_timeout";
    private static final String CURRENT_RUNNING_TIMEOUT = "current_running_timeout";
    private static final String DOWN_STREAM_EFFECT_MAP = "down_stream_effect_map";
    private static final String CURRENT_YEAR_MONTH = "current_year_month";
    private static final String CURRENT_DATA_VERSION_MESSAGE = "current_data_version_message";     

 String message = "当前任务运行失败,是由于执行脚本错误:echo 'hello' ,exist code: 1";
        Map<String, Object> map = Maps.newHashMap();
        map.put(CURRENT_TASK_ID, taskId);
        map.put(CURRENT_TASK_NAME, taskName);
        map.put(CURRENT_TASK_TEAM_NAME, teamName);
        map.put(CURRENT_TASK_ERROR_MESSAGE, title);
        map.put(CURRENT_DATAVERSION_NO, dataVersion);
        map.put(CURRENT_DATAVERSION_LAST_MOFIED, lastModifiedTime);
        map.put(CURRENT_TASK_DUTY_FIRST, firstDutyDetail);
        map.put(CURRENT_TASK_DUTY_SECOND, secondDutyDetail);
        map.put(CURRENT_TASK_DOWN_STREAM_COUNT, downstreamCount);
        map.put(CURRENT_LAUNCHED_TIMEOUT, launchedTimeoutString);
        map.put(CURRENT_RUNNING_TIMEOUT, runingTimeout);
        map.put(DOWN_STREAM_EFFECT_MAP, downStreamEffecMap);
        map.put(CURRENT_DATA_VERSION_MESSAGE, message);
        map.put(CURRENT_YEAR_MONTH, DateTime.now().toString("yyyy.MM"));
        String html = template.getMailText(map);

        System.out.println(html);

 

分享到:
评论

相关推荐

    Spring mvc 发送邮件功能

    基于spring mvc 框架,用freemarker创建邮件模板,包括详细邮件发送功能的spring配置和实现代码

    spring boot 全面的样例代码

    - chapter1:[基本项目构建(可作为工程脚手架),引入web模块,完成一个简单的RESTful API](http://blog.didispace.com/spring-boot-learning-1/) - [使用Intellij中的Spring Initializr来快速构建Spring Boot/...

    spring in action英文版

    第一部分 Spring基础  第1章 开始Spring之旅  1.1 为什么使用Spring  1.1.1 J2EE开发者的一天  1.1.2 Spring的承诺  1.2 Spring是什么  1.3 开始Spring之旅  1.4 理解反向控制  1.4.1 依赖...

    spring4.1核心包

    动作阶段会有“后台”数据改变或动作的代码,这些代码 只会执行一次。显示阶段会产生用户每次刷新时的看到的显示内容。重要的是, 在单个请求的整个处理过程中,动作阶段只会被执行一次,而显示阶段可能会被执行多次...

    Spring-Reference_zh_CN(Spring中文参考手册)

    12.2.4. 不使用回调的基于Spring的DAO实现 12.2.5. 基于Hibernate3的原生API实现DAO 12.2.6. 编程式的事务划分 12.2.7. 声明式的事务划分 12.2.8. 事务管理策略 12.2.9. 容器资源 vs 本地资源 12.2.10. 在应用服务器...

    Spring中文帮助文档

    12.2.4. 不使用回调的基于Spring的DAO实现 12.2.5. 基于Hibernate3的原生API实现DAO 12.2.6. 编程式的事务划分 12.2.7. 声明式的事务划分 12.2.8. 事务管理策略 12.2.9. 容器资源 vs 本地资源 12.2.10. 在应用...

    Spring in Action(第2版)中文版

    2.2.1声明一个简单的bean 2.2.2通过构造函数注入 2.3注入bean属性 2.3.1注入简单的数值 2.3.2使用其他的bean 2.3.3装配集合 2.3.4装配空值 2.4自动装配 2.4.1四种自动装配类型 2.4.2混合使用自动和手动装配...

    Spring API

    12.2.4. 不使用回调的基于Spring的DAO实现 12.2.5. 基于Hibernate3的原生API实现DAO 12.2.6. 编程式的事务划分 12.2.7. 声明式的事务划分 12.2.8. 事务管理策略 12.2.9. 容器资源 vs 本地资源 12.2.10. 在应用...

    Spring in Action(第二版 中文高清版).part2

    第一部分 Spring的核心 第1章 开始Spring之旅 1.1 Spring是什么 1.2 开始Spring之旅 1.3 理解依赖注入 1.3.1 依赖注入 1.3.2 DI应用 1.3.3 企业级应用中的依赖注入 1.4 应用AOP 1.4.1 AOP介绍 1.4.2 AOP...

    Spring in Action(第二版 中文高清版).part1

    第一部分 Spring的核心 第1章 开始Spring之旅 1.1 Spring是什么 1.2 开始Spring之旅 1.3 理解依赖注入 1.3.1 依赖注入 1.3.2 DI应用 1.3.3 企业级应用中的依赖注入 1.4 应用AOP 1.4.1 AOP介绍 1.4.2 AOP...

    Spring 2.0 开发参考手册

    9.9.1. 对一个特定的 DataSource 使用错误的事务管理器 9.10. 更多的资源 10. DAO支持 10.1. 简介 10.2. 一致的异常层次 10.3. 一致的DAO支持抽象类 11. 使用JDBC进行数据访问 11.1. 简介 11.1.1. Spring ...

    spring chm文档

    9.9.1. 对一个特定的 DataSource 使用错误的事务管理器 9.10. 更多的资源 10. DAO支持 10.1. 简介 10.2. 一致的异常层次 10.3. 一致的DAO支持抽象类 11. 使用JDBC进行数据访问 11.1. 简介 11.1.1. Spring ...

    JAVA上百实例源码以及开源项目源代码

    Java从网络取得文件 1个目标文件 简单 Java从压缩包中提取文件 1个目标文件 简单 Java存储与读取对象 1个目标文件 如题 Java调色板面板源代码 1个目标文件 摘要:Java源码,窗体界面,调色板 使用Java语言编写的一款...

    spring-boot-demo_xkcoding.tar.gz

    spring boot demo 是一个用来深度学习并实战 spring boot 的项目,目前总共包含 63 个集成demo,已经完成 52 个。 该项目已成功集成 actuator(监控)、admin(可视化监控)、logback(日志)、aopLog(通过AOP记录web请求...

    spring boot集成demo大全.zip

    `spring boot demo` 是一个用来深度学习并实战 `spring boot` 的项目,目前总共包含 **`63`** 个集成demo,已经完成 **`51`** 个。 该项目已成功集成 actuator(`监控`)、admin(`可视化监控`)、logback(`日志`)、aop...

    springboot参考指南

    发送邮件 xi. 32. 使用JTA处理分布式事务 i. 32.1. 使用一个Atomikos事务管理器 ii. 32.2. 使用一个Bitronix事务管理器 iii. 32.3. 使用一个J2EE管理的事务管理器 iv. 32.4. 混合XA和non-XA的JMS连接 v. 32.5. 支持...

    基于springboot+MyBatis实现的某房产平台系统源码+项目说明(毕设).zip

    Spring Mail + Spring Task完成异步发送激活链接,邮件发送,验证 Jquery BootStrap Ajax springBoot Guava Cache(java工具类集的基础库,注册key的绑定) Druid(监控目的的数据库连接池),配置文件里面...

Global site tag (gtag.js) - Google Analytics