---
title: "email 发送接口"
author: "ysen"
date: "2012-02-29T17:48:57Z"
tags: []
categories: ["企业架构"]
slug: "email"
draft: false
lastmod: "2012-02-29T17:48:56Z"
id: 1432082
url: "https://ysen.iteye.com/blog/1432082"
---

企业邮件发送有自己的发送平台，其它系统包括业务系统常常将发送邮件的任务交给该平台。

该平台通过activemq 建立 queue 接受外围系统的发来的报文格式。

报文格式如下：

"\[{\\"sys\\":\\"fssc\\", \\"key\\":\\"QWESAD456\\", \\"type\\":\\"1\\", \\"email\\":\\"ysen[@xxxx.com\\](mailto:ysen@xxxx.com%5C)", \\"telno\\":\\"13601942660\\", \\"pernr\\":\\"\\", \\"subject\\":\\"mysubject\\", \\"body\\":\\"xxx,您好！&lt;br&gt;截至今日xx点，您还有xxx条待办事项尚未及时处理，&lt;br&gt;其中紧急事项xxx条，请通过以下链接进入系统--“我的待办”页面，尽快处理。谢谢！此邮件通过系统自动发送，请勿直接回复。如您已经处理，请忽略本邮件。&lt;br&gt;mm/dd/yyyy\\", \\"aux\\":\\"020910\\"}]";

sys 对应的外围系统名字

type 对应的是发送方式1.email，2 .短信

key 对应的是密码

 

服务端对这段报文格式先做校验，再做业务处理

客户端需要根据不同的业务制定不同的模板，业务包括body里面的xxx业务数据。

 

这是业务bo，不同的email模板对应不同的msg对象

 

```
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class SendAMQBO {
	private String user = ActiveMQConnection.DEFAULT_USER;
	private String password = ActiveMQConnection.DEFAULT_PASSWORD;
	private String url = "failover://tcp://10.111.11.111:61616";
	private Destination destination;

	public static void main(String[] args) {
		SimpleSendAMQ mySend = new SimpleSendAMQ();
		mySend.sendMessage();
	}

	void sendMessage(Object msg ) {
		try {
			Connection connection = null;
			ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
					user, password, url);
			connection = connectionFactory.createConnection();
			connection.start();
			Session session = connection.createSession(false,
					Session.AUTO_ACKNOWLEDGE);
			destination = session.createQueue("unsQueue");
			MessageProducer producer = session.createProducer(destination);
			producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
			TextMessage message = session.createTextMessage();
			message.setText(msg.toString());
			producer.send(message);

			connection.close();

		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}
}
```

 

 

 

 

 

 

 

msg对象需要一个父类需要对报文格式作json化，包括freemarker加载body模板

```
import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public abstract class EmailTemplate {

	private String sys = "fssc";
	private String key = "QWESAD456";
	private String type = "1";
	private String email = " ";
	private String telno;
	private String pernr;
	private String subject;
	protected String body;
	private String aux;
	protected String templateName = "";
	public static Configuration conf = new Configuration();
	public JsonConfig config = new JsonConfig();

	static {
		conf.setClassForTemplateLoading(EmailTemplate.class, "/templates/");
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getSys() {
		return sys;
	}

	public void setSys(String sys) {
		this.sys = sys;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getPernr() {

		return pernr;
	}

	public void setPernr(String pernr) {
		this.pernr = pernr;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getAux() {
		return aux;
	}

	public void setAux(String aux) {
		this.aux = aux;
	}

	public String getTelno() {
		return telno;
	}

	public void setTelno(String telno) {
		this.telno = telno;
	}

	public String toString() {
		 
		return JSONObject.fromObject(this, config()).toString();
	}

	public void setBody(String body) {
		this.body = body;
	}

	protected JsonConfig config() {

		config.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object object, String name, Object value) {
				Field[] fields = object.getClass().getDeclaredFields();

				for (int i = 0; i < fields.length; i++) {
					if (name.equals(fields[i].getName())) {
						return true;
					}
				}
				
				if (name.equals("templateName")) {
					return true;
				}
				
				return false;

			}

		});
		return config;
	}

	public String setFrBody() {
		try {
			if(null==templateName||"".equals(templateName)){
				templateName = this.getClass().getName()+".ftl";
			}
			
			Template t = conf.getTemplate(templateName, "UTF-8");
			Map<String, Object> root = new HashMap<String, Object>();
			root.put("taskEmail", this);
			StringWriter sw = new StringWriter();
			try {
				t.process(root, sw);
				return sw.toString();

			} catch (TemplateException e) {
				e.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return body;
	}

	public String getBody() {
 
		body = this.setFrBody();

		return body;
	}

	public String getTemplateName() {
		return templateName;
	}

	public void setTemplateName(String templateName) {
		this.templateName = templateName;
	}


}
```

 

 

 

 

 

 

具体业务对象 包含的业务属性不需要做json化，因为它们是包含在body里面的业务数据

 

 

```
public class TaskEmail extends EmailTemplate {

	/**
	 * body contect
	 */

	private String numTask = "2";
	private String urgentTask = "2";
	private String time = "9";

	 
	public static void main(String[] args) {

		TaskEmail taskEmail = new TaskEmail();
		//taskEmail.setTemplateName("TaskEmail.ftl");

		System.out.println(taskEmail.toString());

	}

	public String getNumTask() {
		return numTask;
	}

	public void setNumTask(String numTask) {
		this.numTask = numTask;
	}

	public String getUrgentTask() {
		return urgentTask;
	}

	public void setUrgentTask(String urgentTask) {
		this.urgentTask = urgentTask;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}
	 
}
```

 

 

 

对应的TaskEmail.ftl

```
截至今日${taskEmail.time?if_exists}点，您还有${taskEmail.numTask?if_exists}条待办事项尚未及时处理，其中紧急事项${taskEmail.urgentTask?if_exists}条，<br>请通过以下链接进入系统--“我的待办”页面，尽快处理。谢谢!
```

 

 

 所以添加新的email发送业务只需要添加客户提供的email模板以及包含简单的body属性的对象即可