springside发邮件---javaMail
使用的springside版本3.1.3.2
基于springside团队已经给出的mail代码,做出一下记录:
applicationContext-test.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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<description>
导入的applicationContext文件列表,可随时通过注释disable不需要的模块
</description>
<import resource="email/applicationContext-email.xml" />
<!--<import resource="jmx/applicationContext-jmx-server.xml" />
<import resource="jmx/applicationContext-jmx-client.xml" />
<import resource="schedule/applicationContext-quartz.xml" />
<import resource="schedule/applicationContext-executor.xml" />
<import resource="security/applicationContext-security.xml" />
-->
</beans>
这里面还有一些别的框架,暂时注射掉,留mail用就ok。
applicationContext-mail.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
default-lazy-init="true">
<description>Email配置</description>
<!-- 邮件发送器 -->
<bean id="mailSender"
value="smtp.test.com" />
<property name="username" value="test@yourmail.com" />
<property name="password" value="test" />
<property name="port" value="25"></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<!-- gmail smtp server必须参数 -->
<!--<prop key="mail.smtp.starttls.enable">true</prop>
--></props>
</property>
</bean>
<!-- 文本邮件服务类 -->
<bean id="simpleMailService"
ref="mailSender"></property>
</bean>
<!-- MIME邮件服务类 -->
<bean id="mimeMailService" ref="mailSender" />
<property name="configuration" ref="freemarkerConfiguration"></property>
</bean>
<!--
用于生成HTML邮件内容的Freemarker配置
-->
<bean id="freemarkerConfiguration" value="classpath:/email" />
</bean>
</beans>
simp mail service:
package com.test.service.email;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
/**
* 文本邮件服务类.
*
* @author Bean
*/
@Service
public class SimpleMailService {
static Logger logger = LoggerFactory.getLogger(SimpleMailService.class);
@Autowired
private JavaMailSender mailSender ;
private Executor executor = Executors.newFixedThreadPool(2);
public SimpleMailService(){};
/**
*
* 用Executor多线程群发邮件.
*/
public void sendMail( String fromAddress, String title,
String content,String... sendAddress) {
for (String receiver : sendAddress) {
SimpleMailMessage msg = new SimpleMailMessage();
msg.setFrom(fromAddress);
msg.setTo(receiver);
msg.setSubject(title);
msg.setText(content);
executor.execute(new MailTask(mailSender, msg));
}
}
/**
* 群发邮件任务类.
*/
private static class MailTask implements Runnable {
private JavaMailSender mailSender;
private SimpleMailMessage msg;
public MailTask(JavaMailSender mailSender, SimpleMailMessage msg) {
this.mailSender = mailSender;
this.msg = msg;
}
public void run() {
try {
mailSender.send(msg);
logger.info("纯文本邮件已发送至" + msg.getTo());
} catch (MailException e) {
logger.error("发送邮件失败", e);
}
}
}
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
}
用是线程池。
html格式的邮件发送类:
package com.test.service.email;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import com.ekouw.common.Const;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
/**
* MIME邮件服务类.
*
* 演示由Freemarker引擎生成的,带附件的html格式邮件.
*
* @author calvin
*/
@Service
public class MimeMailService {
private static final String ENCODING = "utf-8";
private static Logger logger = LoggerFactory.getLogger(MimeMailService.class);
private Executor executor = Executors.newFixedThreadPool(2);
@Autowired
private JavaMailSender mailSender;
private Template template;
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
@Autowired
public void setConfiguration(Configuration configuration) throws IOException {
template = configuration.getTemplate("mailTemplate.ftl", ENCODING);
}
/**
* 发送MIME格式的用户修改通知邮件.
*/
public void sendNotificationMail(String fromAddress, String title,
String content,String... sendAddress) {
try {
for (String receiver : sendAddress) {
MimeMessage msg = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg, true, ENCODING);
helper.setTo(receiver);
helper.setFrom(Const.mailUserName);
helper.setSubject(title);
buildContent(helper, content);
executor.execute(new MailTask(mailSender, msg,receiver));
}
} catch (MessagingException e) {
logger.error("构造邮件失败", e);
}
}
/**
* 使用Freemarker生成html格式内容.
*/
@SuppressWarnings("unchecked")
public void buildContent(MimeMessageHelper helper, String cont)throws MessagingException {
try {
Map context = new HashMap();
context.put("context", cont);
String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, context);
helper.setText(content, true);
} catch (IOException e) {
logger.error("构造邮件失败,FreeMarker模板不存在", e);
} catch (TemplateException e) {
logger.error("构造邮件失败,FreeMarker处理失败", e);
}
}
/**
* 群发邮件任务类.
*/
private static class MailTask implements Runnable {
private JavaMailSender mailSender;
private MimeMessage msg2;
private String receiver;
public MailTask(JavaMailSender mailSender, MimeMessage msg2,String receiver) {
this.mailSender = mailSender;
this.msg2 = msg2;
this.receiver = receiver;
}
public void run() {
try {
mailSender.send(msg2);
logger.info("html邮件已发送至"+receiver);
} catch (MailException e) {
logger.error("发送邮件失败", e);
}
}
}
}
和simpl类似。
action中调用:
//mailservice
@Autowired
private SimpleMailService simpleMailService;
@Autowired
private MimeMailService mimeMailService;
//把用户信息发送至邮箱
public String list() throws Exception {
String title = realName+"--邮件发送";
String content = “用户真实姓名: "+realName+"<br>"+"<br>"+
"用户的电话: "+phone+"<br>"+"<br>"+
"用户手机 :"+mobilphone+"<br>"+"<br>"+
"用户的QQ:"+qq+"<br>"+"<br>"+
"用户的邮箱:"+mail+"<br>"+"<br>"+
"用户的详细地址:"+address+"<br>"+"<br>"+
"用户的邮编:"+postcode+"<br>"+"<br>"+
"用户的备注:"+txtRemark;
mimeMailService.sendNotificationMail(Const.serverMailName, title, content, Const.suggestMailName);//html格式邮件
return null;
}
一些参数Const.serverMailName等在常量类中定义。