码迷,mamicode.com
首页 > 编程语言 > 详细

利用Velocity结合Spring发email

时间:2015-09-30 12:42:44      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

在spring中发mail是一件容易的事,如果利用Velocity做mail的模板来发送就更得心应手了。

首先,还是简单描述sping中的配置,发mail需要一个mail的engin:

<bean id="mailEngine" class="com.aodragon.jdt.backend.service.impl.MailEngineImpl">

    <property name="mailSender">

      <ref local="mailSender"/>

    </property>

    <property name="velocityEngine">

      <ref local="velocityEngine"/>

    </property>

  </bean>

实现的类如下:

package com.aodragon.jdt.backend.service.impl;

 

import java.util.Map;

 

import javax.mail.MessagingException;

import javax.mail.internet.MimeMessage;

 

import org.apache.commons.logging.*;

import org.apache.velocity.app.VelocityEngine;

import org.apache.velocity.exception.VelocityException;

 

import org.springframework.core.io.ClassPathResource;

import org.springframework.mail.*;

import org.springframework.mail.javamail.*;

import org.springframework.ui.velocity.VelocityEngineUtils;

import com.aodragon.jdt.backend.service.MailEngine;

 

public class MailEngineImpl implements MailEngine {

    protected static final Log log = LogFactory.getLog(MailEngineImpl.class);

    private MailSender mailSender;

    private VelocityEngine velocityEngine;

 

    public void setMailSender(MailSender mailSender) {

        this.mailSender = mailSender;

    }

 

    public void setVelocityEngine(VelocityEngine velocityEngine) {

        this.velocityEngine = velocityEngine;

    }

 

  

    public void sendMessage(SimpleMailMessage msg, String templateName,

                            Map model) {

        String result = null;

 

        try {

            result =                VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateName, model);

        } catch (VelocityException e) {

            e.printStackTrace();

        }

 

        msg.setText(result);

        send(msg);

    }

   

    public void sendMessage(SimpleMailMessage msg, String templateName, String encoding, Map model) {

        String result = null;

        try {

            result =                VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateName, encoding,model);

        } catch (VelocityException e) {

            e.printStackTrace();

        }

 

        msg.setText(result);

        send(msg);

    }

 

    public void send(SimpleMailMessage msg) {

        try {

            mailSender.send(msg);

        } catch (MailException ex) {

            //log it and go on

            log.error(ex.getMessage());

        }

    }

 

   

    public void sendMessage(String[] emailAddresses,

                            ClassPathResource resource, String bodyText,

                            String subject, String attachmentName)

    throws MessagingException {

        MimeMessage message =

            ((JavaMailSenderImpl) mailSender).createMimeMessage();

 

        // use the true flag to indicate you need a multipart message

        MimeMessageHelper helper = new MimeMessageHelper(message, true);

 

        helper.setTo(emailAddresses);

        helper.setText(bodyText);

        helper.setSubject(subject);

 

        helper.addAttachment(attachmentName, resource);

 

        ((JavaMailSenderImpl) mailSender).send(message);

    }

}

这个engin实现了4个方法,在需要的时候调用,这里也需要两个对象:

private MailSender mailSender;

    private VelocityEngine velocityEngine;

所以在配置的时候需要注意有:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">

    <property name="host">

      <value>${email.host}</value>

    </property>

    <property name="username">

      <value>${email.username}</value>

    </property>

    <property name="password">

      <value>${email.password}</value>

    </property>

  </bean>

<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"/>

      </props>

    </property>

  </bean>

这样配置是完成了,下面给一个在Action中发mail的调用示例

public ActionForward save(ActionMapping actionMapping,

                              ActionForm actionForm,

                              HttpServletRequest httpServletRequest,

                              HttpServletResponse httpServletRresponse) throws

        Exception {

 

       

        String toEmail = "…"

HashMap model = new HashMap();

        this.smm.setTo(toEmail);

        this.smm.setSubject("谢谢您的关注");

        model.put("content","………….. ");

        MessageResources resources = getResources(httpServletRequest);

        this.mailEngin.sendMessage(this.smm,resources.getMessage(_locale,"velocity.email.bookproduct"),model);

 

        return list(actionMapping, actionForm, httpServletRequest,

                    httpServletRresponse);

    } // end save

这里需要说明的就是调用sendMessage中的第2个参数为resources.getMessage(_locale,"velocity.email.bookproduct"),这里是为了适应多语言版本,调用对应的vm文件,velocity.email.bookproduct是保存在ApplicationResource.properties各个版本中的vm文件对应的路径。其中_locale = (Locale)request.getSession().getAttribute(Globals.LOCALE_KEY)。

这样,简单的Velocity结合Spring发送email就完成了,还可以添加一个TASK来管理邮件的队列,让系统定时发送队列中的邮件;在线管理mailSender中的各个参数等等。

 

利用Velocity结合Spring发email

标签:

原文地址:http://www.cnblogs.com/jinzhiming/p/4848714.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!