使用spring发送邮件

本文主要介绍使用Spring3.x框架中的JavaMail支持来实现邮件发送功能,并添加附件。

1.需要用到的jar包:mail-1.4.jar,spring
如果使用maven,pom.xml中添加依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<properties>
<!-- 依赖公共包版本定义-->
<log4j.version>1.2.14</log4j.version>
<spring.version>3.1.0.RELEASE</spring.version>
</properties>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>

2.spring-mail配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
>
<!-- 注意:这里的参数(如用户名、密码)都是针对邮件发送者的 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<!-- SMTP发送邮件的服务器的IP和端口 -->
<property name="host" value="${mail.host}" />
<property name="port" value="${mail.port}" />

<!-- 登陆SMTP邮件发送服务器的用户名和密码 -->
<property name="username" value="${mail.username}" />
<property name="password" value="${mail.password}" />

<!-- 获得邮件会话属性,验证登录邮件服务器是否成功 -->
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="prop">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
</props>
</property>
</bean>

<!-- 定时任务:每月2号发送邮件 corn:秒 分钟 小时 天(月) 月 天(星期) 年份 -->
<task:scheduled-tasks>
<task:scheduled ref="taskJob" method="sendMailJob" cron="0 0 0 2 * ? *" /> <!-- 每月2号的凌晨 -->
</task:scheduled-tasks>

<context:component-scan base-package="cn.com.starit.kanms.task" />

</beans>

properties 文件配置

1
2
3
4
5
6
7
8
9
10
# 发送邮件配置
mail.host=smtp.163.com
mail.port=25
mail.username=maxinchun0215@163.com
mail.password=********
# 接收邮件的邮箱,多个用','分割
# 发送给
mail.to=maxinchun0215@qq.com,ma.xinchun@ustcinfo.com
# 抄送给
mail.copy=ha.chuanchi@ustcinfo.com,zhong.jinkai@ustcinfo.com

3.java类分别为
SendMailUtil类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package cn.com.starit.kanms.util.mail;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import cn.com.starit.commons.service.ApplicationContextProvider;
import cn.com.starit.commons.web.util.ExportUtil;
import cn.com.starit.kanms.dao.GridDao;
import cn.com.starit.kanms.service.DataSerive;
import cn.com.starit.kanms.util.CustomizedPropertyPlaceholderConfigurer;

import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;

/**
* 发送邮件工具
*
* @author maxinchun
*
*/

public class SendMailUtil {
private static Logger logger = Logger.getLogger(SendMailUtil.class);
private static JavaMailSender sender;
// 获取JavaMailSender bean
static {
if (sender == null) {
sender = (JavaMailSender) ApplicationContextProvider.getBean("mailSender");
}
}


/**
* 以附件形式发送email
* @param sendTo 收件人email地址
* @param copyTo 抄送对象的email地址
* @param mailSubject 邮件主题
* @param mailBody 内容
* @param files 附件
*/

public static void sendFileMail(String[] sendTo,String[] copyTo, String mailSubject, String mailBody, File[] files) {

MimeMessage mimeMessage = sender.createMimeMessage();
try {
if(logger.isDebugEnabled()){
logger.debug("带附件和图片的邮件正在发送...");
}
// 设置utf-8或GBK编码,否则邮件会有乱码
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
// 设置发送人名片
String from = (String)CustomizedPropertyPlaceholderConfigurer.getContextProperty("mail.username");
helper.setFrom(from);
// 设置收件人邮箱
helper.setTo(sendTo);
if(copyTo.length != 0 && !copyTo[0].equals("")){
helper.setCc(copyTo);
}

// 主题
helper.setSubject(mailSubject);
// 邮件内容,注意加参数true,表示启用html格式
helper.setText(mailBody,true);

if (files != null && files.length > 0) {
for (int i = 0; i < files.length; i++)
// 加入附件
helper.addAttachment(MimeUtility.encodeText(files[i]
.getName()), files[i]);
}
// 加入插图(图片附件)
// helper.addInline(MimeUtility.encodeText("pic01"), new File("E:\\test\\slide-5.png"));

sender.send(mimeMessage);
logger.info("带附件和图片的邮件发送成功!");
} catch (Exception e) {
logger.info("带附件和图片的邮件发送失败!!!");
e.printStackTrace();
}

}

public static String createExecl(Long id) {
GridDao gridDao = DataSerive.getGridDao();
try {
Map<String,Object> map = gridDao.getGridInfoByGridId(id);
if(map !=null){
String fileName = map.get("TITLE").toString();
String columns = map.get("COLMN").toString();
String sql = map.get("SQL").toString();
List dataList = gridDao.getDataInfo(sql);
String path = ExportUtil.createExcel(dataList, columns, fileName);
return path;
}else{
logger.info("没有需要的导出数据,请查看girdId:【"+id+"】是否正确");
}
} catch (Exception e) {
logger.info("生成excel失败!!!!");
e.printStackTrace();
}
return "";

}

public static void main(String[] args) throws Exception {

File noPay = new File(SendMailUtil.createExecl(143L));
File toPay = new File(SendMailUtil.createExecl(142L));

// File noPay = new File("E:\\test\\无交费记录接入点_20160105040144.xls");
// File toPay = new File("E:\\test\\需要交费接入点_20160105050105.xls");
// String to = "maxinchun0215@qq.com"; //ma.xinchun@ustcinfo.com
String subject = "需要交费的接入点信息";
String[] sendTo = CustomizedPropertyPlaceholderConfigurer.getContextProperty("mail.to").toString().split(",");
String[] copyTo = CustomizedPropertyPlaceholderConfigurer.getContextProperty("mail.copy").toString().split(",");
String body = "<html><head></head><body><h1>您好:</h1>&nbsp;&nbsp;附件为本月需要交费的接入点信息。其中,附件1为系统中暂时还<span style='color:red'>没有交费记录</span>的接入点信息;附件2为已超时但还<span style='color:red'>未交费</span>的接入点信息<br>提示:本邮件为系统每月自动发送</body></html>";
// String body = "您好,附件为需要交费的接入点信息。其中,附件1为系统中暂时还没有交费记录的接入点信息;附件2为已超时但还未交费的接入点信息。请查收(提示:本邮件为每月自动发送)";
File[] files = new File[]{noPay,toPay};
SendMailUtil.sendFileMail(sendTo, copyTo, subject, body, files);
}
}

ApplicationContextProvider类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package cn.com.starit.commons.service;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class ApplicationContextProvider {

private static ApplicationContext context = null;

/**
*
* @methodName 得到spring访问类
* @return
* @author ymiao
* @DateTime Aug 27, 2008
* @since 1.0.0
*/
public static ApplicationContext getContext(){

if(context == null){

initContext(null);
}

return context;
}

/**
*
* @methodName 得到spring访问类
* @param contextFilePath 配置文件名称
* @return
* @author ymiao
* @DateTime Aug 27, 2008
* @since 1.0.0
*/
public static ApplicationContext getContext(String contextFilePath){
if(context == null)
initContext(contextFilePath);
return context;
}



/**
* @methodName 得到实列bean
* @param beanName Bean名称
* @return
* @author ymiao
* @DateTime Aug 27, 2008
* @since 1.0.0
*/
public static Object getBean(String beanName)
{
return getContext().getBean(beanName);
}

/**
* @methodName 得到实列bean
* @param beanName Bean名称
* @param clsType Bean 类
* @return
* @author ymiao
* @DateTime Aug 27, 2008
* @since 1.0.0
*/
public static Object getBean(String beanName, Class clsType)
{
return getContext().getBean(beanName, clsType);
}

/**
*
* @methodName:初始化spring
* @param contextFilePath
* @author ymiao
* @DateTime Aug 27, 2008
* @since 1.0.0
*/
private static void initContext(String contextFilePath){
if(contextFilePath == null)
contextFilePath = "applicationContext.xml";
context = new ClassPathXmlApplicationContext(contextFilePath);
}
}

CustomizedPropertyPlaceholderConfigurer类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package cn.com.starit.kanms.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class CustomizedPropertyPlaceholderConfigurer extends
PropertyPlaceholderConfigurer {

private static Map<String, Object> ctxPropertiesMap;

@Override
protected void processProperties(
ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throws BeansException {

super.processProperties(beanFactoryToProcess, props);
ctxPropertiesMap = new HashMap<String, Object>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
ctxPropertiesMap.put(keyStr, value);
}
}

public static Object getContextProperty(String name) {
return ctxPropertiesMap.get(name);
}

}

热评文章