由于项目里要发邮件,用了spring 和 velocity 模板。写下来以后可以看,好记性不如博客。
版本说明下:
spring 2.0.6
velocity 1.5
javamail 用 spring/lib下的
1.封装邮件信息类:
package com.chenlb.mail;
import java.util.Map;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.velocity.VelocityEngineUtils;
/**
* 邮件发送器
* @作者 chenlb
* @创建时间 2007-7-28 下午03:35:31
*/
public class VelocityTemplateMailMessage {
protected final Log logger = LogFactory.getLog(getClass());
private JavaMailSender javaMailSender;
private VelocityEngine velocityEngine;
private String from;
private String title;
private String encoding;
private String templateLocation;
private String[] toEmails;
private Map model;
public boolean send() {
MimeMessage msg = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg);
try {
helper.setFrom(from);
helper.setSubject(title);
helper.setTo(toEmails);
helper.setText(getMessage(), true); //如果发的不是html内容去掉true参数
javaMailSender.send(msg);
} catch (MessagingException e) {
// TODO 自动生成 catch 块
if(logger.isWarnEnabled()) {
logger.warn("邮件信息导常! 邮件标题为: "+title);
}
return false;
//e.printStackTrace();
} catch (MailException me) {
// TODO: handle exception
if(logger.isWarnEnabled()) {
logger.warn("发送邮件失败! 邮件标题为: "+title);
}
return false;
}
return true;
}
/**
* 邮件模板中得到信息
* @return 返回特发送的内容
*/
private String getMessage() {
return VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
templateLocation, encoding, model);
}
private String[] createToEmail(String to) {
return new String[] {to};
}
public void setToEmail(String to) {
setToEmails(createToEmail(to));
}
public void setJavaMailSender(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
public void setVelocityEngine(VelocityEngine velocityEngine) {
this.velocityEngine = velocityEngine;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
public void setModel(Map model) {
this.model = model;
}
public void setTemplateLocation(String templateLocation) {
this.templateLocation = templateLocation;
}
public void setTitle(String title) {
this.title = title;
}
public void setToEmails(String[] toEmails) {
this.toEmails = toEmails;
}
public void setFrom(String from) {
this.from = from;
}
public String getTemplateLocation() {
return templateLocation;
}
}
2.spring配置文件,applictionContext-mail.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"dtd/spring-beans-2.0.dtd">
<!-- http://www.springframework.org/ -->
<beans>
<!-- 属性文件加载 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:mail.properties</value>
</list>
</property>
</bean>
<!-- 邮件发送器 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}" />
<property name="username" value="${mail.username}" />
<property name="password" value="${mail.password}" />
<property name="defaultEncoding" value="UTF-8"></property>
<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>
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="resourceLoaderPath" value="classpath:velocity"></property>
</bean>
<bean id="templateMail" class="com.chenlb.mail.VelocityTemplateMailMessage">
<property name="javaMailSender" ref="mailSender"></property>
<property name="from" value="${mail.from}"></property>
<property name="encoding" value="UTF-8"></property>
<property name="templateLocation" value="test.vm"></property>
<property name="velocityEngine" ref="velocityEngine"></property>
<property name="title" value="wwww.blogjava.net/chenlb"></property>
</bean>
</beans>
说明:模板文件放到classpath的velocity目录下,可自行改。
3.发送者邮件信息,mail.properties(classpath位置):
mail.from=yourname@126.com
mail.host=smtp.126.com
mail.password=yourpassword
mail.smtp.auth=true
mail.smtp.timeout=25000
mail.username=yourname
4.模板文件,text.vm(classpath的velocity目录下):
你好!${me} 这是模板生成的邮件。
5.使用:
VelocityTemplateMailMessage vtmm = (VelocityTemplateMailMessage) context.getBean("templateMail");
Map<String, String> data = new HashMap<String, String>();
data("me","yourname");
vtmm.setModel(data);
vtmm.setToMail("yourOtherMail@163.com");
vtmm.setTitle("mail with veloctiy and spring");
vtmm.send();
看下是否收到邮件了。^_^
posted on 2007-08-21 19:43
流浪汗 阅读(3325)
评论(6) 编辑 收藏 所属分类:
OpenSource