10
Dec
09

Automated Email using Spring Framework and Velocity


This page describes the process one can take to send automated emails based on a velocity template, Maven and the Spring Framework. The page also covers how to send email with inline resources. These emails have the images included in the message as mime attachments.

Requirements

  • Velocity Template Library
  • Spring Framework
  • Maven
  • SMTP port 25 access (eg. mail.yourhostname.com)

This page requires you have access to a mail server that can accept mail from your machine. You may typically test this by running telnet mail.yourhostname.com 25. Test this by manually sending a message and verify that it gets sent.

Start the project

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart

groupId: com.test
artifactId: test-email

Hit enter for the rest of the questions leaving them default.

After creating the project edit the pom.xml file and add the following dependencies.

        <dependency>
          <groupId>javax.mail</groupId>
          <artifactId>mail</artifactId>
          <version>1.4</version>
        </dependency>
        <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring</artifactId>
                <version>2.5.6</version>
        </dependency>
        <dependency>
                <groupId>velocity</groupId>
                <artifactId>velocity</artifactId>
                <version>1.5</version>
        </dependency>

Creating a template

The first step in the process is to create a email template. The template contains email layout. This is similar to creating a JSP page.

<html>
<body>
<h3>Hi ${user.userName}, welcome!</h3>

<div>
   Your email address is <a href="mailto:${user.emailAddress}">${user.emailAddress}</a>.
</div>
</body>
</html>

Configuration

Since we are using the autowiring functionality of the spring framework we dont really need to explicitly define the beans in xml. Instead all we need are the next few lines.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<!-- The following is used for the @Autowired and @Required, @Resource etc... -->
        <context:annotation-config/>
        <context:component-scan base-package="com.test"/>

   <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
      <property name="host" value="mail.hostname.com"/>
   </bean>

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

Creating the application

The following is the code to a simple java main application that will call the method that sends out the email.

package com.test;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
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("app")
public class App {
        private JavaMailSender mailSender;
        private VelocityEngine velocityEngine;

        public JavaMailSender getMailSender() {
                return mailSender;
        }

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

        public VelocityEngine getVelocityEngine() {
                return velocityEngine;
        }

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

        public static void main(String[] args) {
                // initialize spring context
                ApplicationContext context = new ClassPathXmlApplicationContext(
                                new String[] { "applicationContext.xml" });
                App app = (App) context.getBean("app");
                // call the method
                app.testEmail();
        }

        public void testEmail() {
                final User user = new User();
                user.setEmailAddress("neeraj.verma@hostname.com");
                user.setUserName("verma");

                MimeMessagePreparator preparator = new MimeMessagePreparator() {
                        public void prepare(MimeMessage mimeMessage) throws Exception {
                                MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                                message.setTo(user.getEmailAddress());
                                message.setFrom("neeraj.verma@hostname.com"); // could be
                                // parameterized...
                                Map model = new HashMap();
                                model.put("user", user);
                                String text = VelocityEngineUtils.mergeTemplateIntoString(
                                                velocityEngine, "com/test/email-template.vm", model);
                                message.setText(text, true);
                        }
                };
                this.mailSender.send(preparator);
        }
}

User

package com.test;

public class User {
        private String userName;
        private String emailAddress;

        public String getUserName() {
                return userName;
        }
        public void setUserName(String userName) {
                this.userName = userName;
        }
        public String getEmailAddress() {
                return emailAddress;
        }
        public void setEmailAddress(String emailAddress) {
                this.emailAddress = emailAddress;
        }
}

Running the code

run the code by executing the following command.

mvn exec:java -Dexec.mainClass="com.test.App"

In-line resources using Velocity Templates.

Change template to include images

<html>
<body>
<h3>Hi ${user.userName}, welcome!</h3>

<div>
   Your email address is <a href="mailto:${user.emailAddress}">${user.emailAddress}</a>.
</div>
<br/>
<img src="cid:identifier1234"/>
</body>
</html>

Convert to send multi-part messages

        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true); // multipart

Add the image(s)

Be careful with the order in which you add the resources. It may not work if it is not ordered “correctly”.

        FileSystemResource res = new FileSystemResource(new File(
                        "/home/verma/Desktop/wallpaper/wright.jpg"));
        message.addInline("identifier1234", res);

This site is a collaborative effort! The complete text and sourcecode for this is available on GitHub. Corrections and enhancements are welcome, please make the change and submit a pull request in the comment area below.
Advertisements

2 Responses to “Automated Email using Spring Framework and Velocity”


  1. 2 Chandraprakash
    February 22, 2013 at 3:19 am

    Thanks for such a wonderful post.It makes my work easier.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 75 other followers

December 2009
S M T W T F S
« Nov   Jan »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Blog Stats

  • 813,810 hits

%d bloggers like this: