1、spring + Java Mail + Velocity
项目结构:
注意:用户包中引入各包的顺序问题。如velocity-2.1。
beans.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- spring配置文件的根元素,使用spring-beans-4.0.xsd语义约束 --> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="mail.properties"/> </bean> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="${mail.host}"/> <property name="username" value="${mail.user}"/> <property name="password" value="${mail.pwd}"/> </bean> <bean id="javaMailSenderService" class="com.lfy.sendmail.JavaMailSenderService"/> <bean id="javaMailSenderImplService" class="com.lfy.sendmail.JavaMailSenderImplService"/> <!-- VelocityEngineFactory --> <bean id="velocityEngineFactory" 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> </props> </property> </bean> <bean id="velocityEngineService" class="com.lfy.velocity.VelocityEngineService"/> </beans>
VelocityEngineService.java
package com.lfy.velocity; import org.apache.velocity.app.VelocityEngine; import org.springframework.beans.factory.annotation.Autowired; public class VelocityEngineService { @Autowired private VelocityEngine velocityEngine; public VelocityEngine getVelocityEngine() { return velocityEngine; } public void setVelocityEngine(VelocityEngine velocityEngine) { this.velocityEngine = velocityEngine; } }
JavaMailSenderImplService.java
package com.lfy.sendmail; import javax.mail.internet.MimeMessage; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; /** * 2、spring支持的第二种发送器JavaMailSenderImpl * @author lfy * */ public class JavaMailSenderImplService { @Autowired private JavaMailSenderImpl javaMailSenderImpl; public JavaMailSenderImpl getJavaMailSenderImpl() { return javaMailSenderImpl; } public void setJavaMailSenderImpl(JavaMailSenderImpl javaMailSenderImpl) { this.javaMailSenderImpl = javaMailSenderImpl; } /** * simple content * @param message */ public void send(SimpleMailMessage message){ javaMailSenderImpl.send(message); System.out.println("JavaMailSenderImpl:send silpleMessage successfully."); } /** * Velocity content * @param message */ public void sendWithVelocity(MimeMessage message) { javaMailSenderImpl.send(message); System.out.println("JavaMailSenderImpl:send mimeMessage successfully."); } }
index.vm
<html> <head> <style type="text/css"> h4{ color:red; background:#efefef; } </style> </head> <body> <h4>${user} </h4> <p><p> <i>${content}</i> </body> </html>
springJavaMailSender.java
package com.lfy.main; import java.util.HashMap; import java.util.Map; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.apache.velocity.app.VelocityEngine; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.ui.velocity.VelocityEngineUtils; import com.lfy.sendmail.JavaMailSenderImplService; import com.lfy.velocity.VelocityEngineService; public class springJavaMailSender { public static void main(String[] agrs) throws MessagingException { //创建spring容器 ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml"); JavaMailSenderImplService javaMailSenderImplService = (JavaMailSenderImplService)ctx.getBean("javaMailSenderImplService"); VelocityEngine velocityEngine=(VelocityEngine)((VelocityEngineService)ctx.getBean("velocityEngineService")).getVelocityEngine(); Map<String,Object> model=new HashMap<String,Object>(); model.put("user", "Tomcat"); model.put("content", "Hello"); String emailText=VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "index.vm", "UTF-8", model); MimeMessage msg=javaMailSenderImplService.getJavaMailSenderImpl().createMimeMessage(); MimeMessageHelper helper=new MimeMessageHelper(msg,true); helper.setFrom("xxxxxxxxxx@163.com"); helper.setTo("xxxxxxxx@qq.com"); helper.setCc("xxxxxxxx@163.com"); helper.setSubject("Velocity模板测试"); helper.setText(emailText, true); javaMailSenderImplService.sendWithVelocity(msg); } }
运行效果:
2、使用Maven配置使用Velocity。整理中...
3、Velocity加载模板的3中方式
1》从文件加载模板文件(默认方式)
properties.setProperty("resource.loader", "file"); //设置velocity资源加载方式为file时的处理类 properties.setProperty("file.resource.loader.class","org.apache.velocity.runtime.resource.loader.FileResourceLoader");
2》从类路径加载模板文件
properties.setProperty("resource.loader", "class"); //设置velocity资源加载方式为file时的处理类 properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
3》从jar文件中加载模板文件
properties.setProperty("resource.loader", "jar"); //设置velocity资源加载方式为file时的处理类 properties.setProperty("jar.resource.loader.class", "org.apache.velocity.runtime.resource.loader.JarResourceLoader"); //设置jar包所在的位置 properties.setProperty("jar.resource.loader.path", "jar:file:/F:/quicksure_Server_Provider.jar");