Spring+Mail发送邮件

   Spring+Mail常用类:

MailSend
 接口,只支持文本和HTML -->JavaMailSenderImpl
JavaMailSender  接口,支持文本和HTML,附件->JavaMailSenderImpl
MimeMessage  发送普通邮件消息类
MimeMessageHelper  发送附件邮件消息类
freemarker.template.Configuration FreeMarkerConfigurationFactoryBean(实现)

 

   Spring+Mail发送邮件:

package com.spring.email;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class Mail {

	/**
	 * java 发送邮件 
	 * org.springframework.mail.javamail.JavaMailSender 接口
	 */
	private JavaMailSender mailSender;
	
	/**
	 * FreeMarker 配置
	 */
	private Configuration config;

	/**
	 * 获取
	 * 使用模板发邮件
	 * @param fileName 模板名称
	 * @param molde 动态对象
	 * @return 模板 文本、HTML
	 * @throws Exception
	 */
	public String getTemplateText(String fileName,Object... molde) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		for (Object object : molde) {
			String name = object.getClass().getSimpleName();
			name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toLowerCase());
			map.put(name,object);
		}
		Template template = config.getTemplate(fileName);
		return FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
	}
	
	/**
	 * 发送邮件
	 * @param bean 邮件属性
	 * @throws MessagingException
	 * @throws UnsupportedEncodingException 
	 */
	public void send(MailBean bean) throws MessagingException, UnsupportedEncodingException{
		MimeMessage msg = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(msg,true,"UTF-8");
		msg.setSubject(bean.getSubject());
		helper.setSentDate(bean.getDate());
		helper.setText(bean.getText());
		helper.setReplyTo(bean.getRelyTO());
		helper.setFrom(bean.getFrom());
		for (File file : bean.getFiles()) {
			helper.addAttachment(MimeUtility.encodeWord(file.getName()), file);
		}
		for (String bcc : bean.getBcc()) {
			helper.addBcc(bcc);
		}
		for (String cc : bean.getCc()) {
			helper.addBcc(cc);
		}
		for (String to : bean.getTos()) {
			helper.addTo(to);
		}
		mailSender.send(msg);
	}
	
	/**
	 * 发送邮件  HTML 格式
	 * @param bean 邮件属性
	 * @throws MessagingException
	 * @throws UnsupportedEncodingException 
	 */
	public void sendHTML(MailBean bean) throws MessagingException, UnsupportedEncodingException{
		MimeMessage msg = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(msg,true,"UTF-8");
		msg.setSubject(bean.getSubject());
		helper.setSentDate(bean.getDate());
		helper.setText(bean.getText(),true);
		helper.setReplyTo(bean.getRelyTO());
		helper.setFrom(bean.getFrom());
		for (File file : bean.getFiles()) {
			helper.addAttachment(MimeUtility.encodeWord(file.getName()), file);
		}
		for (String bcc : bean.getBcc()) {
			helper.addBcc(bcc);
		}
		for (String cc : bean.getCc()) {
			helper.addBcc(cc);
		}
		for (String to : bean.getTos()) {
			helper.addTo(to);
		}
		mailSender.send(msg);
	}
	
	/**
	 * 发送邮件  HTML模板 格式
	 * @param bean 邮件属性
	 * @throws MessagingException
	 * @throws UnsupportedEncodingException 
	 */
	public void sendTemplate(MailBean bean,String fileName,Object... molder) throws Exception{
		MimeMessage msg = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(msg,true,"UTF-8");
		msg.setSubject(bean.getSubject());
		helper.setSentDate(bean.getDate());
		helper.setText(getTemplateText(fileName,molder),true);
		helper.setReplyTo(bean.getRelyTO());
		helper.setFrom(bean.getFrom());
		for (File file : bean.getFiles()) {
			helper.addAttachment(MimeUtility.encodeWord(file.getName()), file);
		}
		for (String bcc : bean.getBcc()) {
			helper.addBcc(bcc);
		}
		for (String cc : bean.getCc()) {
			helper.addBcc(cc);
		}
		for (String to : bean.getTos()) {
			helper.addTo(to);
		}
		mailSender.send(msg);
	}
	
	public JavaMailSender getMailSender() {
		return mailSender;
	}

	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	public Configuration getConfig() {
		return config;
	}

	public void setConfig(Configuration config) {
		this.config = config;
	}
}

   实体类 MailBean:

package com.spring.email;

import java.io.File;
import java.util.Date;

public class MailBean {

	private String from;
	
	private String[] tos = {};
	
	private String[] bcc = {};
	
	private String[] cc = {};
	
	private String relyTO;
	
	private String text;
	
	private String subject;
	
	private Date date;
	
	private File[] files = {}; 
//省略 getter,setter
}

 

   Spring 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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
	<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="port" value="25"/>
		<property name="protocol" value="smtp"/>
		<property name="host" value="123.com"/>
		<property name="username" value="111111"/>
		<property name="password" value="111111"/>
		<property name="defaultEncoding" value="utf-8"/>
		<property name="javaMailProperties">
			<props>
				<prop key="mail.smtp.auth">true</prop>
			</props>
		</property>
	</bean>
	<bean id="freeMarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
		<property name="freemarkerSettings">
			<props>
				<prop key="default_encoding">UTF-8</prop>
				<prop key="template_update_delay">600000</prop>
			</props>
		</property>
		<property name="templateLoaderPath" value="com/spring/freemaker"/>
	</bean>
	<bean class="com.spring.email.Mail">
		<property name="mailSender" ref="javaMailSender"/>
		<property name="config" ref="freeMarkerConfiguration"/>
	</bean>
	<bean id="dateFormat" class="java.text.SimpleDateFormat">
		<constructor-arg value="yyyy-MM-dd"/>
	</bean>
</beans>

 

你可能感兴趣的:(Spring+Mail发送邮件)