View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.service;
24  
25  import java.util.Map;
26  
27  import javax.mail.MessagingException;
28  import javax.mail.internet.MimeMessage;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.velocity.app.VelocityEngine;
33  import org.apache.velocity.exception.VelocityException;
34  import org.springframework.core.io.ClassPathResource;
35  import org.springframework.mail.MailException;
36  import org.springframework.mail.MailSender;
37  import org.springframework.mail.SimpleMailMessage;
38  import org.springframework.mail.javamail.JavaMailSenderImpl;
39  import org.springframework.mail.javamail.MimeMessageHelper;
40  import org.springframework.ui.velocity.VelocityEngineUtils;
41  
42  /**
43   * Class for sending e-mail messages based on Velocity templates or with
44   * attachments.
45   * 
46   * @author Matt Raible
47   */
48  public class MailEngine {
49      private final Log log = LogFactory.getLog(MailEngine.class);
50  
51      private MailSender mailSender;
52  
53      private VelocityEngine velocityEngine;
54  
55      public void setMailSender(MailSender mailSender) {
56  	this.mailSender = mailSender;
57      }
58  
59      public void setVelocityEngine(VelocityEngine velocityEngine) {
60  	this.velocityEngine = velocityEngine;
61      }
62  
63      /**
64       * Send a simple message based on a Velocity template.
65       * 
66       * @param msg
67       *                the message to populate
68       * @param templateName
69       *                the Velocity template to use (relative to classpath)
70       * @param model
71       *                a map containing key/value pairs
72       */
73      @SuppressWarnings("unchecked")
74      public void sendMessage(SimpleMailMessage msg, String templateName,
75  	    Map model) {
76  	String result = null;
77  
78  	try {
79  	    result = VelocityEngineUtils.mergeTemplateIntoString(
80  		    velocityEngine, templateName, model);
81  	} catch (VelocityException e) {
82  	    log.error(e.getMessage());
83  	}
84  
85  	msg.setText(result);
86  	send(msg);
87      }
88  
89      /**
90       * Send a simple message with pre-populated values.
91       * 
92       * @param msg
93       *                the message to send
94       */
95      public void send(SimpleMailMessage msg) {
96  	try {
97  	    mailSender.send(msg);
98  	} catch (MailException ex) {
99  	    // log it and go on
100 	    log.error(ex.getMessage());
101 	}
102     }
103 
104     /**
105      * Convenience method for sending messages with attachments.
106      * 
107      * @param recipients
108      *                array of e-mail addresses
109      * @param sender
110      *                e-mail address of sender
111      * @param resource
112      *                attachment from classpath
113      * @param bodyText
114      *                text in e-mail
115      * @param subject
116      *                subject of e-mail
117      * @param attachmentName
118      *                name for attachment
119      * @throws MessagingException
120      *                 thrown when can't communicate with SMTP server
121      */
122     public void sendMessage(String[] recipients, String sender,
123 	    ClassPathResource resource, String bodyText, String subject,
124 	    String attachmentName) throws MessagingException {
125 	MimeMessage message = ((JavaMailSenderImpl) mailSender)
126 		.createMimeMessage();
127 
128 	// use the true flag to indicate you need a multipart message
129 	MimeMessageHelper helper = new MimeMessageHelper(message, true);
130 
131 	helper.setTo(recipients);
132 	helper.setFrom(sender);
133 	helper.setText(bodyText);
134 	helper.setSubject(subject);
135 
136 	helper.addAttachment(attachmentName, resource);
137 
138 	((JavaMailSenderImpl) mailSender).send(message);
139     }
140 }