由于項目里要發郵件,用了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.tkk7.com/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
流浪汗 閱讀(3330)
評論(6) 編輯 收藏 所屬分類:
OpenSource