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);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值