Friday, October 22, 2010

Spring v3.0.2 Learning Note 15 - Use Velocity Email Template with Spring

go on with Learning Note 14 - Send Email With Spring

Reference Library

need add the following reference library files for velocity

%SPRING_DEP%\org.apache.commons\com.springsource.org.apache.commons.collections\3.2.1\com.springsource.org.apache.commons.collections-3.2.1.jar
%SPRING_DEP%\org.apache.commons\com.springsource.org.apache.commons.lang\2.1.0\com.springsource.org.apache.commons.lang-2.1.0.jar
%SPRING_DEP%\org.apache.velocity\com.springsource.org.apache.velocity\1.5.0\com.springsource.org.apache.velocity-1.5.0.jar
 %SPRING_DEP%\org.apache.velocity\com.springsource.org.apache.velocity.tools.view\1.4.0\com.springsource.org.apache.velocity.tools.view-1.4.0.jar

Email Template File

//account.vm

Dear Customer,

Account No. :<strong>${accountId}</strong>
Password : ${password}

Now you are allowed to access:

<li><a href=${url1}>yahoo</a></li>
<li><a href=${url2}>google</a></li>

XXX Administrator


Spring Configuration

<!-- Configure Velocity for sending e-mail -->
    <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>
            </props>
        </property>
    </bean>


Implement Class

package test.spring.email;

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

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;

import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Component;
import org.springframework.ui.velocity.VelocityEngineUtils;

@Component
public class AccountNotifierVelocityImpl implements AccountNotifier {

    @Resource
    private JavaMailSender mailSender;
    @Resource
    private SimpleMailMessage mailMessage;

    public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    public void setMailMessage(SimpleMailMessage mailMessage) {
        this.mailMessage = mailMessage;
    }

    @Resource
    private VelocityEngine velocityEngine;

    public void setVelocityEngine(VelocityEngine velocityEngine) {
        this.velocityEngine = velocityEngine;
    }

    @Override
    public void notifyAccount(final String accountId, final String password) {

        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,
                        true);
                helper.setFrom(mailMessage.getFrom());
                helper.setTo(mailMessage.getTo());
                helper.setSubject(mailMessage.getSubject());

                // pass value for all parameters
                Map<String, String> model = new HashMap<String, String>();
                model.put("accountId", accountId);
                model.put("password", password);
                model.put("url1", "http://www.google.com");
                model.put("url2", "http://www.baidu.com");
                String result = null;
                try {
                    // account.vm must be in your classpath
                    result = VelocityEngineUtils.mergeTemplateIntoString(
                            velocityEngine, "account.vm", model).replaceAll(
                            "\n", "<br/>");
                    helper.setText(result, true);
                } catch (VelocityException e) {
                    e.printStackTrace();
                }
            }
        };
        mailSender.send(preparator);
    }
}


Junit Test


package test.spring.email;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class EmailTester {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Test
    public void testEmailSender() {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "spring.xml");

        AccountNotifier accountNotifier = (AccountNotifier) context
                .getBean("accountNotifierVelocityImpl");
        accountNotifier.notifyAccount("Account123", "pwd12345");
    }
}

No comments:

Post a Comment