KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > service > MailEngine


1 package org.appfuse.service;
2
3 import java.util.Map JavaDoc;
4
5 import javax.mail.MessagingException JavaDoc;
6 import javax.mail.internet.MimeMessage JavaDoc;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.apache.velocity.app.VelocityEngine;
11 import org.apache.velocity.exception.VelocityException;
12
13 import org.springframework.core.io.ClassPathResource;
14 import org.springframework.mail.MailException;
15 import org.springframework.mail.MailSender;
16 import org.springframework.mail.SimpleMailMessage;
17 import org.springframework.mail.javamail.JavaMailSenderImpl;
18 import org.springframework.mail.javamail.MimeMessageHelper;
19 import org.springframework.ui.velocity.VelocityEngineUtils;
20
21 /**
22  * Class for sending e-mail messages based on Velocity templates
23  * or with attachments.
24  *
25  * <p><a HREF="MailEngine.java.htm"><i>View Source</i></a></p>
26  *
27  * @author Matt Raible
28  */

29 public class MailEngine {
30     protected static final Log log = LogFactory.getLog(MailEngine.class);
31     private MailSender mailSender;
32     private VelocityEngine velocityEngine;
33
34     public void setMailSender(MailSender mailSender) {
35         this.mailSender = mailSender;
36     }
37
38     public void setVelocityEngine(VelocityEngine velocityEngine) {
39         this.velocityEngine = velocityEngine;
40     }
41
42     /**
43      * Send a simple message based on a Velocity template.
44      * @param msg
45      * @param templateName
46      * @param model
47      */

48     public void sendMessage(SimpleMailMessage msg, String JavaDoc templateName,
49                             Map JavaDoc model) {
50         String JavaDoc result = null;
51
52         try {
53             result =
54                 VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
55                                                             templateName, model);
56         } catch (VelocityException e) {
57             e.printStackTrace();
58         }
59
60         msg.setText(result);
61         send(msg);
62     }
63
64     /**
65      * Send a simple message with pre-populated values.
66      * @param msg
67      */

68     public void send(SimpleMailMessage msg) {
69         try {
70             mailSender.send(msg);
71         } catch (MailException ex) {
72             //log it and go on
73
log.error(ex.getMessage());
74         }
75     }
76
77     /**
78      * Convenience method for sending messages with attachments.
79      *
80      * @param emailAddresses
81      * @param resource
82      * @param bodyText
83      * @param subject
84      * @param attachmentName
85      * @throws MessagingException
86      * @author Ben Gill
87      */

88     public void sendMessage(String JavaDoc[] emailAddresses,
89                             ClassPathResource resource, String JavaDoc bodyText,
90                             String JavaDoc subject, String JavaDoc attachmentName)
91     throws MessagingException JavaDoc {
92         MimeMessage JavaDoc message =
93             ((JavaMailSenderImpl) mailSender).createMimeMessage();
94
95         // use the true flag to indicate you need a multipart message
96
MimeMessageHelper helper = new MimeMessageHelper(message, true);
97
98         helper.setTo(emailAddresses);
99         helper.setText(bodyText);
100         helper.setSubject(subject);
101
102         helper.addAttachment(attachmentName, resource);
103
104         ((JavaMailSenderImpl) mailSender).send(message);
105     }
106 }
107
Popular Tags