JavaMail与Velocity制作邮件模板

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" " http://www.springframework.org/dtd/spring-beans.dtd ">
<beans default-autowire="byName">
 <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
         <property name="host" value="smtp.163.com"/>
         <property name="username" value="[email protected]"/>
         <property name="password" value="013651"/>
         <property name="javaMailProperties">
             <props>
                 <prop key="mail.smtp.auth">true</prop>
                 <prop key="mail.smtp.timeout">25000</prop>
             </props>
         </property>
     </bean>

     <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
         <property name="from" value="[email protected]"/>
         <property name="replyTo" value="[email protected]"/>
         <property name="cc" value="[email protected]"/>
         <property name="to" value="[email protected],[email protected]"/>
    </bean>

     <bean id="mailService" class="org.frame.base.mail.MailService">
         <property name="mailSender" ref="mailSender"/>
         <property name="mailMessage" ref="mailMessage"/>
     </bean>

     <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
		<property name="resourceLoaderPath" value="classpath:template"/>
     </bean>

     <bean id="velocityMailService" class="org.frame.base.mail.VelocityMailService">
     	   <property name="mailSender" ref="mailSender"/>
      	   <property name="mailMessage" ref="mailMessage"/>
      	   <property name="template" value="test.vm"/>
      	   <property name="velocityEngine" ref="velocityEngine"/>
     </bean>
</beans>

废话就不多说了,代码如上.Velocity发送邮件的代码如下:
package org.frame.base.mail;

import java.util.HashMap;
import java.util.Map;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.mail.MailMessage;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.ui.velocity.VelocityEngineUtils;

public class VelocityMailService {

	MailSender mailSender;
	MailMessage mailMessage;
	String template;
	VelocityEngine velocityEngine;
	Map model = new HashMap();
	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}
	public void setMailMessage(MailMessage mailMessage) {
		this.mailMessage = mailMessage;
	}
	public void setTemplate(String template) {
		this.template = template;
	}
	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}

	@SuppressWarnings("unchecked")
	public void send(String subject){
		model.put("me", "杨春龙");
		mailMessage.setSubject(subject);
		mailMessage.setText(getText());
		mailSender.send((SimpleMailMessage) mailMessage);
	}

	public String getText(){
		return VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, template, model);
	}

}


通过Velocity渲染后,返回邮件内容,并发送邮件

你可能感兴趣的:(java,spring,UI,bean,velocity)