在我们发送邮件时,如果要发送一个页面,最好的方式就是利用模板的方式。模板方式是利用一个velocity或freemaker的页面为模板,可以自己设置一些参数,然后将此模板发送给接受者。这里我们利用velocity发送模板邮件。
使用velocity创建邮件模板,需要用到velocity库,所以用户在使用前,将velocity库加入到类路径中。
下面是实现代码:
1.spring的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:flex="http://www.springframework.org/schema/flex"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
http://www.springframework.org/schema/flex
http://www.springframework.org/schema/flex/spring-flex-1.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- <context:property-placeholder location="email.properties"/> -->
<context:component-scan base-package="com.chh.mail.template"/>
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<value>
resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</value>
</property>
</bean>
</beans>
2.发送代码
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.ui.velocity.VelocityEngineUtils;
@Component
public class VelocityEmailService {
@Autowired
private VelocityEngine velocityEngine;
public void sendEmail() throws MessagingException {
//创建并设置发送服务
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setUsername("xxxxxx@163.com");
javaMailSender.setPassword("aaaaaaa");
javaMailSender.setHost("smtp.163.com");
Properties pp = new Properties();
pp.setProperty("mail.smtp.auth", "true");
javaMailSender.setJavaMailProperties(pp);
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,
true, "utf-8");// 信息工具
messageHelper.setFrom("caohuihe@163.com");// 设置源
messageHelper.setTo("caohuihe@yy.com");// 设置到达地址
messageHelper.setSubject("vm test");// 主题
Map<String, Object> model = new HashMap<String, Object>();
model.put("userName", "少保");
model.put("emailAddress", "shaobao@163.com");
String text = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, "com/chh/mail/template/welcome.vm", "utf-8",
model);//根据页面及参数生成 关于此网页的字符串
messageHelper.setText(text, true);
javaMailSender.send(mimeMessage);// 发送邮件
}
}
3.测试代码
import javax.mail.MessagingException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class VelocityEmailTest {
public static void main(String[] args) throws MessagingException
{
ApplicationContext context =
new ClassPathXmlApplicationContext("com/chh/mail/template/bean.xml");
VelocityEmailService tves = context.getBean(VelocityEmailService.class);
tves.sendEmail();
}
}
上面已经是根据前人写的代码,最简单的实现形式。