發送纯文本的郵件:
备注:velocity为classpath中存放模板的文件夹
velocity中存在的email模板text.vm
text.vm内容如下:
你好!${me} 这是模板生成的邮件。
重点代码如下:
package cn.com.unusap.unutrip.spring.email;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.ui.velocity.VelocityEngineUtils;
/**
*
* @author longgangbai
*
*/
public class VelocityTemplateMailMessage {
protected static final Log log = LogFactory
.getLog(VelocityTemplateMailMessage.class);
private MailSender mailSender;
private SimpleMailMessage message;
private VelocityEngine velocityEngine;
public void setVelocityEngine(VelocityEngine velocityEngine) {
this.velocityEngine = velocityEngine;
}
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
public void setMessage(SimpleMailMessage message) {
this.message = message;
}
public void sendEmail(Map model) {
message.setTo("wuxc@unutrip.com");
message.setSubject("subject");
String result = null;
try {
result = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, "/velocity/text.vm", model);
//model存储模板中使用的对象的
} catch (VelocityException e) {
e.printStackTrace();
}
message.setText(result);
mailSender.send(message);
}
}
applicationContext-velocity.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">
<!-- For mail settings and future properties files -->
<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}</value>
</property>
<property name="username">
<value>${mail.username}</value>
</property>
<property name="password">
<value>${mail.password}</value>
</property>
</bean>
<bean id="mailMessage"
class="org.springframework.mail.SimpleMailMessage">
<property name="from">
<value>${mail.from}</value>
</property>
</bean>
<!-- Configure Velocity for sending e-mail 制定發送郵件的模板的编码格式使用utf-8支持中文 -->
<bean id="velocityEngine"
class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<props>
<prop key="resource.loader">class</prop>
<prop key="class.resource.loader.class">
org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</prop>
<prop key="velocimacro.library"></prop>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding">UTF-8</prop>
<prop key="default.contentType">
text/html; charset=UTF-8
</prop>
</props>
</property>
</bean>
<bean id="templateMail"
class="cn.com.unusap.unutrip.spring.email.VelocityTemplateMailMessage">
<property name="mailSender" ref="mailSender" />
<property name="message" ref="mailMessage" />
<property name="velocityEngine" ref="velocityEngine" />
</bean>
</beans>
测试代码:
ApplicationContext appctx = new ClassPathXmlApplicationContext(
"/applicationContext-velocity.xml");
RegistrationService vtmm = (RegistrationService) appctx
.getBean("registrationService");
Order order = new Order();
order.setContent("xiao bai");
order.setDate(new Date());
order.setEmail("wuxc@unutrip.com");
order.setOrderId("N0001");
order.setUsername("wu xiaochao ");
vtmm.register(order);
郵件配置:
classpath路径下mail.properties内容如下:
mail.from=longgangbai@xxxx.com
mail.host=mail.xxx.com
mail.password=xxxx
mail.smtp.auth=true
mail.smtp.timeout=25000
mail.username=xxxxx