Coverage Report - org.jtheque.core.utils.MailUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
MailUtils
0 %
0/32
0 %
0/6
1.6
 
 1  
 package org.jtheque.core.utils;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.utils.bean.Email;
 20  
 
 21  
 import javax.activation.DataHandler;
 22  
 import javax.activation.DataSource;
 23  
 import javax.activation.FileDataSource;
 24  
 import javax.mail.BodyPart;
 25  
 import javax.mail.Message;
 26  
 import javax.mail.MessagingException;
 27  
 import javax.mail.Multipart;
 28  
 import javax.mail.Session;
 29  
 import javax.mail.Transport;
 30  
 import javax.mail.internet.InternetAddress;
 31  
 import javax.mail.internet.MimeBodyPart;
 32  
 import javax.mail.internet.MimeMessage;
 33  
 import javax.mail.internet.MimeMultipart;
 34  
 import java.io.File;
 35  
 import java.util.Date;
 36  
 import java.util.Properties;
 37  
 
 38  
 /**
 39  
  * Utility class for send mail.
 40  
  *
 41  
  * @author Baptiste Wicht
 42  
  */
 43  
 public final class MailUtils {
 44  
     /**
 45  
      * Construct a new MailUtils. This class isn't instanciable.
 46  
      */
 47  
     private MailUtils() {
 48  0
         super();
 49  0
     }
 50  
 
 51  
     /**
 52  
      * Send an email.
 53  
      *
 54  
      * @param email The email to send.
 55  
      * @param host  The host to send the message.
 56  
      * @throws MessagingException Can throw this exception if an exception occurs during the send of the mail
 57  
      */
 58  
     public static void send(Email email, String host) throws MessagingException {
 59  0
         MimeMessage msg = createMessage(host);
 60  
 
 61  0
         configureMessage(email, msg);
 62  
 
 63  0
         Multipart mp = new MimeMultipart();
 64  
 
 65  0
         MimeBodyPart mbp1 = new MimeBodyPart();
 66  0
         mbp1.setText(email.getMessage());
 67  
 
 68  0
         mp.addBodyPart(mbp1);
 69  
 
 70  0
         if (!email.getAttachedFiles().isEmpty()) {
 71  0
             attachFiles(email, mp);
 72  
         }
 73  
 
 74  0
         msg.setContent(mp);
 75  
 
 76  0
         Transport.send(msg);
 77  0
     }
 78  
 
 79  
     /**
 80  
      * Configure the email.
 81  
      *
 82  
      * @param email The email to get the infos from.
 83  
      * @param msg   The msg to configure.
 84  
      * @throws MessagingException Throw if an error occurs during the treatment.
 85  
      */
 86  
     private static void configureMessage(Email email, MimeMessage msg) throws MessagingException {
 87  0
         msg.setFrom(new InternetAddress(email.getFrom()));
 88  
 
 89  0
         for (String to : email.getTo()) {
 90  0
             msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
 91  
         }
 92  
 
 93  0
         msg.setSubject(email.getSubject());
 94  0
         msg.setSentDate(new Date());
 95  0
     }
 96  
 
 97  
     /**
 98  
      * Create a message with a specific host.
 99  
      *
 100  
      * @param host The host to use.
 101  
      * @return The mime message.
 102  
      */
 103  
     private static MimeMessage createMessage(String host) {
 104  0
         Properties props = new Properties();
 105  0
         props.setProperty("mail.smtp.host", host);
 106  
 
 107  0
         Session session = Session.getInstance(props, null);
 108  0
         session.setDebug(false);
 109  
 
 110  0
         return new MimeMessage(session);
 111  
     }
 112  
 
 113  
     /**
 114  
      * Attach files to the message.
 115  
      *
 116  
      * @param email The email to get the files from.
 117  
      * @param mp    The multi part message.
 118  
      * @throws MessagingException Throw if an error occurs during the attaching files process.
 119  
      */
 120  
     private static void attachFiles(Email email, Multipart mp) throws MessagingException {
 121  0
         for (File f : email.getAttachedFiles()) {
 122  0
             BodyPart messageBodyPart = new MimeBodyPart();
 123  
 
 124  0
             DataSource source = new FileDataSource(f);
 125  0
             messageBodyPart.setDataHandler(new DataHandler(source));
 126  0
             messageBodyPart.setFileName(source.getName());
 127  
 
 128  0
             mp.addBodyPart(messageBodyPart);
 129  0
         }
 130  0
     }
 131  
 }