Coverage Report - org.kuali.rice.core.mail.Mailer
 
Classes in this File Line Coverage Branch Coverage Complexity
Mailer
0%
0/67
0%
0/32
5.667
 
 1  
 /*
 2  
  * Copyright 2006-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.core.mail;
 17  
 
 18  
 import org.springframework.mail.MailException;
 19  
 import org.springframework.mail.SimpleMailMessage;
 20  
 import org.springframework.mail.javamail.JavaMailSenderImpl;
 21  
 
 22  
 import javax.activation.DataHandler;
 23  
 import javax.mail.Address;
 24  
 import javax.mail.Message;
 25  
 import javax.mail.MessagingException;
 26  
 import javax.mail.internet.AddressException;
 27  
 import javax.mail.internet.InternetAddress;
 28  
 import javax.mail.internet.MimeMessage;
 29  
 import javax.mail.util.ByteArrayDataSource;
 30  
 import java.io.IOException;
 31  
 
 32  
 
 33  
 /**
 34  
  * Maintains a Java Mail session and is used for sending e-mails.
 35  
  *
 36  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 37  
  */
 38  0
 public class Mailer {
 39  
 
 40  0
             protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(Mailer.class);
 41  
 
 42  
             private JavaMailSenderImpl mailSender;          
 43  
             
 44  
                 /**
 45  
                  * @param mailSender The injected Mail Sender.
 46  
                  */
 47  
                 public void setMailSender(JavaMailSenderImpl mailSender) {
 48  0
                         this.mailSender = mailSender;
 49  0
                 }
 50  
             
 51  
                 /**
 52  
              * Construct and a send simple email message from a Mail Message.
 53  
              * 
 54  
              * @param message
 55  
              *            the Mail Message
 56  
                  * @throws MessagingException 
 57  
              */
 58  
                 @SuppressWarnings("unchecked")
 59  
                 public void sendEmail(MailMessage message) throws MessagingException {
 60  
                 
 61  
                 // Construct a simple mail message from the Mail Message
 62  0
                 SimpleMailMessage smm = new SimpleMailMessage();
 63  0
                 smm.setTo( (String[])message.getToAddresses().toArray(new String[message.getToAddresses().size()]) );
 64  0
                 smm.setBcc( (String[])message.getBccAddresses().toArray(new String[message.getBccAddresses().size()]) );
 65  0
                 smm.setCc( (String[])message.getCcAddresses().toArray(new String[message.getCcAddresses().size()]) );
 66  0
                 smm.setSubject(message.getSubject());
 67  0
                 smm.setText(message.getMessage());
 68  0
                 smm.setFrom(message.getFromAddress());
 69  
 
 70  
                 try {
 71  0
                         if ( LOG.isDebugEnabled() ) {
 72  0
                                 LOG.debug( "sendEmail() - Sending message: " + smm.toString() );
 73  
                         }
 74  0
                     mailSender.send(smm);
 75  
                 }
 76  0
                 catch (Exception e) {
 77  0
                         LOG.error("sendEmail() - Error sending email.", e);
 78  0
                                 throw new RuntimeException(e);
 79  0
                 }
 80  0
             }
 81  
                 
 82  
                 /**
 83  
              * Send an email to a single recipient with the specified subject and message. This is a convenience 
 84  
              * method for simple message addressing.
 85  
              * 
 86  
              * @param from
 87  
              *            sender of the message            
 88  
              * @param to
 89  
              *            list of addresses to which the message is sent
 90  
              * @param subject
 91  
              *            subject of the message
 92  
              * @param body
 93  
              *            body of the message
 94  
              */
 95  
                 public void sendEmail(EmailFrom from, EmailTo to, EmailSubject subject, EmailBody body, boolean htmlMessage) {
 96  0
                 if (to.getToAddress() == null) {
 97  0
                     LOG.warn("No To address specified. Refraining from sending mail.");
 98  0
                     return;
 99  
                 }
 100  
                         try {
 101  0
                         Address[] singleRecipient = {new InternetAddress(to.getToAddress())};
 102  0
                                 sendMessage(from.getFromAddress(),
 103  
                                                     singleRecipient,
 104  
                                                     subject.getSubject(),
 105  
                                                     body.getBody(), 
 106  
                                                     null,
 107  
                                                     null,
 108  
                                                     htmlMessage);
 109  0
                         } catch (Exception e) {
 110  0
                                 LOG.error("sendEmail(): ", e);
 111  0
                                 throw new RuntimeException(e);
 112  0
                         }
 113  0
                 }
 114  
 
 115  
                 /**
 116  
              * Send an email to the given "to", "cc", and "bcc" recipients with the specified subject and message.
 117  
              * 
 118  
              * @param from
 119  
              *            sender of the message            
 120  
              * @param to
 121  
              *            list of addresses to which the message is sent
 122  
              * @param subject
 123  
              *            subject of the message
 124  
              * @param body
 125  
              *            body of the message
 126  
              * @param cc
 127  
              *            list of addresses which are to be cc'd on the message
 128  
              * @param bc
 129  
              *            list of addresses which are to be bcc'd on the message
 130  
              */
 131  
                 public void sendEmail(EmailFrom from, EmailToList to, EmailSubject subject, EmailBody body, EmailCcList cc, EmailBcList bc, boolean htmlMessage) {
 132  0
                     if (to.getToAddresses().isEmpty()) {
 133  0
                                 LOG.error("List of To addresses must contain at least one entry. Refraining from sending mail.");
 134  0
                                 return;
 135  
                     }
 136  
                         try {
 137  0
                             sendMessage(from.getFromAddress(), 
 138  
                                             to.getToAddressesAsAddressArray(), 
 139  
                                             subject.getSubject(), 
 140  
                                             body.getBody(), 
 141  
                                                         (cc == null ? null : cc.getToAddressesAsAddressArray()), 
 142  
                                                         (bc == null ? null : bc.getToAddressesAsAddressArray()), 
 143  
                                                         htmlMessage);
 144  0
                         } catch (Exception e) {
 145  0
                                 LOG.error("sendEmail(): ", e);
 146  0
                                 throw new RuntimeException(e);
 147  0
             }
 148  0
                 }
 149  
                 
 150  
                 /**
 151  
              * Send an email to the given recipients with the specified subject and message.
 152  
              * 
 153  
              * @param from
 154  
              *            sender of the message            
 155  
              * @param to
 156  
              *            list of addresses to which the message is sent
 157  
              * @param subject
 158  
              *            subject of the message
 159  
              * @param messageBody
 160  
              *            body of the message
 161  
              * @param cc
 162  
              *            list of addresses which are to be cc'd on the message
 163  
              * @param bcc
 164  
              *            list of addresses which are to be bcc'd on the message
 165  
              */
 166  
             protected void sendMessage(String from, Address[] to, String subject, String messageBody, Address[] cc, Address[] bcc, boolean htmlMessage) throws AddressException, MessagingException, MailException {
 167  0
                     MimeMessage message = mailSender.createMimeMessage();
 168  
 
 169  
                 // From Address
 170  0
                 message.setFrom(new InternetAddress(from));
 171  
 
 172  
                 // To Address(es)
 173  0
                 if (to != null && to.length > 0) {
 174  0
                     message.addRecipients(Message.RecipientType.TO, to);
 175  
                 } else {
 176  0
                     LOG.error("No recipients indicated.");
 177  
                 }
 178  
 
 179  
                 // CC Address(es)
 180  0
                 if (cc != null && cc.length > 0) {
 181  0
                     message.addRecipients(Message.RecipientType.CC, cc);
 182  
                 }
 183  
 
 184  
                 // BCC Address(es)
 185  0
                 if (bcc != null && bcc.length > 0) {
 186  0
                     message.addRecipients(Message.RecipientType.BCC, bcc);
 187  
                 }
 188  
 
 189  
                 // Subject
 190  0
                 message.setSubject(subject);
 191  0
                 if (subject == null || "".equals(subject)) {
 192  0
                     LOG.warn("Empty subject being sent.");
 193  
                 }
 194  
 
 195  
                 // Message body.
 196  0
                 if (htmlMessage) {
 197  0
                     prepareHtmlMessage(messageBody, message);
 198  
                 } else {
 199  0
                     message.setText(messageBody);
 200  0
                     if (messageBody == null || "".equals(messageBody)) {
 201  0
                         LOG.warn("Empty message body being sent.");
 202  
                     }
 203  
                 }
 204  
 
 205  
                 // Send the message
 206  
                 try {
 207  0
                         mailSender.send(message);
 208  
                 }
 209  0
                 catch (Exception e) {
 210  0
                         LOG.error("sendMessage(): ", e);
 211  0
                         throw new RuntimeException(e);
 212  0
                 }
 213  0
             }
 214  
 
 215  
             protected void prepareHtmlMessage(String messageText, Message message) throws MessagingException {
 216  
                 try {
 217  0
                                 message.setDataHandler(new DataHandler(new ByteArrayDataSource(messageText, "text/html")));
 218  0
                         } catch (IOException e) {
 219  0
                                 LOG.warn(e.getMessage());
 220  0
                                 throw new RuntimeException(e);
 221  0
                         }
 222  0
             }
 223  
 }