SSM 框架整合 spring 发送email邮件

  |   0 评论   |   0 浏览

1.pom引入依赖

	<dependency>
		<groupId>javax.mail</groupId>
		<artifactId>mail</artifactId>
		<version>1.4.7</version>
	</dependency>

2.创建 spring-context-email.xml

<?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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"
	default-lazy-init="true">

	<!-- 加载配置属性文件 -->
	<context:property-placeholder ignore-unresolvable="true" location="classpath:app.properties" />

    <!--配置邮件接口-->
    <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${mail.smtp.host}"/>
        <property name="username" value="${mail.smtp.username}"/>
        <property name="password" value="${mail.smtp.password}"/>
        <property name="defaultEncoding" value="${mail.smtp.defaultEncoding}"/>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
                <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
            </props>
        </property>
    </bean>
</beans>

3.app.properties文件内容(邮箱信息自行注册获取)

mail.smtp.host=smtp.163.com
#username
mail.smtp.username=demo@163.com
#password
mail.smtp.password=demo123
#code
mail.smtp.defaultEncoding=utf-8
#flag
mail.smtp.auth=true
#timeout
mail.smtp.timeout=20000

4.读取properties配置文件 工具类





import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * 读取properties配置文件
 * @author ssk
 * @date 2012-12-26 下午1:39:35
 *
 */
public class ConfUtil {

	public static Logger log = Logger.getLogger(ConfUtil.class);
	private static Properties p = new Properties();

	/**
	 * 加载properties文件
	 */
	static {
		try {
			p.load(ConfUtil.class.getClassLoader().getResourceAsStream("app.properties"));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			log.debug("properties 文件加载失败");
		}
	}

	/**
	 * 通过key,获取properties文件的值
	 * @param key 键
	 * @return
	 */
	public static String getValue(String key) {
		log.debug(p.getProperty(key));
		return p.getProperty(key);
	}

	/**
	 * 设置properties文件的键值对
	 * @param key 键
	 * @param value 值
	 * @param rootPath properties文件跟路径
	 */
	public static void setValue(String key, String value, String rootPath) {
		p.setProperty(key, value);
		try {
			String appPath = rootPath + File.separator + "WEB-INF" + File.separator + "classes" 
						+ File.separator + "app.properties";
			OutputStream out = new FileOutputStream(appPath);
			p.store(out, "update " + key + " value");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			log.error("系统错误:" + e.getMessage());
		}
	}
}

5.SendEmailUtil.java 工具类




import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

public class SendEmailUtil {

	private static JavaMailSender javaMailSender = SpringContextHolder.getBean(JavaMailSender.class);

	/**
	 * 发送邮件(发件人昵称,收件人,标题,内容)
	 */
	public static void sendMsg(String nickName,String[] addressee,String title,String msg){
		SimpleMailMessage message = new SimpleMailMessage();
	    //发件人信息
	    message.setFrom(nickName+"<"+ConfUtil.getValue("mail.smtp.username")+">");
	    //收件人的邮箱地址(String[]数组)
	    message.setTo(addressee);
	    //邮件主题
	    message.setSubject(title);
	    //邮件内容
	    message.setText(msg);
	    //发送
	    mySend(message);
	   
	}

	/**
	 * 发送邮件主体方法
	 * @param message
	 */
	private static void mySend(SimpleMailMessage message){
		//最多重试10次
		for(int i=0;i<10;i++){
			try {
				//发送邮件
				javaMailSender.send(message);
				return;
			} catch (Exception e) {
				if(i==9){
					e.printStackTrace();
				}

			}
		}
	}

}