鐗堟湰璇存槑涓嬶細(xì)
spring 2.0.6
velocity 1.5
javamail 鐢?spring/lib涓嬬殑
1.灝佽閭歡淇℃伅綾伙細(xì)
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;
/**
* 閭歡鍙戦佸櫒
* @浣滆?nbsp;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 鍧?/span>
if(logger.isWarnEnabled()) {
logger.warn("閭歡淇℃伅瀵煎父! 閭歡鏍囬?shù)? "+title);
}
return false;
//e.printStackTrace();
} catch (MailException me) {
// TODO: handle exception
if(logger.isWarnEnabled()) {
logger.warn("鍙戦侀偖浠跺け璐? 閭歡鏍囬?shù)? "+title);
}
return false;
}
return true;
}
/**
* 閭歡妯℃澘涓緱鍒頒俊鎭?br> * @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閰嶇疆鏂囦歡錛宎pplictionContext-mail.xml錛?br>
<?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>
<!-- 灞炴ф枃浠跺姞杞?nbsp;-->
<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.aygfsteel.com/chenlb"></property>
</bean>
</beans>
璇存槑錛氭ā鏉挎枃浠舵斁鍒癱lasspath鐨剉elocity鐩綍涓嬶紝鍙嚜琛屾敼銆?br>
3.鍙戦佽呴偖浠朵俊鎭紝mail.properties(classpath浣嶇疆)錛?br>
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.妯℃澘鏂囦歡錛宼ext.vm(classpath鐨剉elocity鐩綍涓?錛?br>
浣犲ソ錛?{me} 榪欐槸妯℃澘鐢熸垚鐨勯偖浠躲?/span>
5.浣跨敤錛?br>
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();
鐪嬩笅鏄惁鏀跺埌閭歡浜?jiǎn)銆俕_^

]]>