View Javadoc

1   package net.ramapuram.thomas.service;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   import org.apache.velocity.app.VelocityEngine;
6   import org.apache.velocity.exception.VelocityException;
7   import org.springframework.core.io.ClassPathResource;
8   import org.springframework.mail.MailException;
9   import org.springframework.mail.MailSender;
10  import org.springframework.mail.SimpleMailMessage;
11  import org.springframework.mail.javamail.JavaMailSenderImpl;
12  import org.springframework.mail.javamail.MimeMessageHelper;
13  import org.springframework.ui.velocity.VelocityEngineUtils;
14  
15  import javax.mail.MessagingException;
16  import javax.mail.internet.MimeMessage;
17  import java.util.Map;
18  
19  /**
20   * Class for sending e-mail messages based on Velocity templates
21   * or with attachments.
22   * 
23   * @author Matt Raible
24   */
25  public class MailEngine {
26      private final Log log = LogFactory.getLog(MailEngine.class);
27      private MailSender mailSender;
28      private VelocityEngine velocityEngine;
29      private String defaultFrom;
30  
31      public void setMailSender(MailSender mailSender) {
32          this.mailSender = mailSender;
33      }
34  
35      public MailSender getMailSender() {
36          return mailSender;
37      }
38  
39      public void setVelocityEngine(VelocityEngine velocityEngine) {
40          this.velocityEngine = velocityEngine;
41      }
42  
43      public void setFrom(String from) {
44          this.defaultFrom = from;
45      }
46  
47      /**
48       * Send a simple message based on a Velocity template.
49       * @param msg the message to populate
50       * @param templateName the Velocity template to use (relative to classpath)
51       * @param model a map containing key/value pairs
52       */
53      public void sendMessage(SimpleMailMessage msg, String templateName, Map model) {
54          String result = null;
55  
56          try {
57              result =
58                  VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
59                                                              templateName, model);
60          } catch (VelocityException e) {
61              e.printStackTrace();
62              log.error(e.getMessage());
63          }
64  
65          msg.setText(result);
66          send(msg);
67      }
68  
69      /**
70       * Send a simple message with pre-populated values.
71       * @param msg the message to send
72       * @throws org.springframework.mail.MailException when SMTP server is down
73       */
74      public void send(SimpleMailMessage msg) throws MailException {
75          try {
76              mailSender.send(msg);
77          } catch (MailException ex) {
78              log.error(ex.getMessage());
79              throw ex;
80          }
81      }
82  
83      /**
84       * Convenience method for sending messages with attachments.
85       * 
86       * @param recipients array of e-mail addresses
87       * @param sender e-mail address of sender
88       * @param resource attachment from classpath
89       * @param bodyText text in e-mail
90       * @param subject subject of e-mail
91       * @param attachmentName name for attachment
92       * @throws MessagingException thrown when can't communicate with SMTP server
93       */
94      public void sendMessage(String[] recipients, String sender, 
95                              ClassPathResource resource, String bodyText,
96                              String subject, String attachmentName)
97      throws MessagingException {
98          MimeMessage message = ((JavaMailSenderImpl) mailSender).createMimeMessage();
99  
100         // use the true flag to indicate you need a multipart message
101         MimeMessageHelper helper = new MimeMessageHelper(message, true);
102 
103         helper.setTo(recipients);
104 
105         // use the default sending if no sender specified
106         if (sender == null) {
107             helper.setFrom(defaultFrom);
108         } else {
109            helper.setFrom(sender);
110         }
111 
112         helper.setText(bodyText);
113         helper.setSubject(subject);
114 
115         helper.addAttachment(attachmentName, resource);
116 
117         ((JavaMailSenderImpl) mailSender).send(message);
118     }
119 }