Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 182   Methods: 2
NCLOC: 92   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MimeMailer.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 package org.apache.tools.ant.taskdefs.email;
 55   
 
 56   
 import java.io.ByteArrayOutputStream;
 57   
 import java.io.File;
 58   
 import java.io.IOException;
 59   
 import java.io.PrintStream;
 60   
 import java.io.UnsupportedEncodingException;
 61   
 import java.util.Enumeration;
 62   
 import java.util.Properties;
 63   
 import java.util.Vector;
 64   
 import javax.activation.DataHandler;
 65   
 import javax.activation.FileDataSource;
 66   
 import javax.mail.Message;
 67   
 import javax.mail.MessagingException;
 68   
 import javax.mail.Session;
 69   
 import javax.mail.Transport;
 70   
 import javax.mail.internet.AddressException;
 71   
 import javax.mail.internet.InternetAddress;
 72   
 import javax.mail.internet.MimeBodyPart;
 73   
 import javax.mail.internet.MimeMessage;
 74   
 import javax.mail.internet.MimeMultipart;
 75   
 import org.apache.tools.ant.BuildException;
 76   
 
 77   
 /**
 78   
  * Uses the JavaMail classes to send Mime format email.
 79   
  *
 80   
  * @author roxspring@yahoo.com Rob Oxspring
 81   
  * @since Ant 1.5
 82   
  */
 83   
 class MimeMailer extends Mailer {
 84   
     /** Sends the email  */
 85  0
     public void send() {
 86  0
         try {
 87  0
             Properties props = new Properties();
 88   
 
 89  0
             props.put("mail.smtp.host", host);
 90  0
             props.put("mail.smtp.port", String.valueOf(port));
 91   
 
 92   
             // Aside, the JDK is clearly unaware of the scottish
 93   
             // 'session', which //involves excessive quantities of
 94   
             // alcohol :-)
 95  0
             Session sesh = Session.getDefaultInstance(props, null);
 96   
 
 97   
             //create the message
 98  0
             MimeMessage msg = new MimeMessage(sesh);
 99  0
             MimeMultipart attachments = new MimeMultipart();
 100   
 
 101   
             //set the sender
 102  0
             if (from.getName() == null) {
 103  0
                 msg.setFrom(new InternetAddress(from.getAddress()));
 104   
             } else {
 105  0
                 msg.setFrom(new InternetAddress(from.getAddress(),
 106   
                     from.getName()));
 107   
             }
 108   
 
 109  0
             msg.setRecipients(Message.RecipientType.TO,
 110   
                 internetAddresses(toList));
 111  0
             msg.setRecipients(Message.RecipientType.CC,
 112   
                 internetAddresses(ccList));
 113  0
             msg.setRecipients(Message.RecipientType.BCC,
 114   
                 internetAddresses(bccList));
 115   
 
 116  0
             if (subject != null) {
 117  0
                 msg.setSubject(subject);
 118   
             }
 119  0
             msg.addHeader("Date", getDate());
 120   
 
 121  0
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 122  0
             PrintStream out = new PrintStream(baos);
 123   
 
 124  0
             message.print(out);
 125  0
             out.close();
 126   
 
 127  0
             MimeBodyPart textbody = new MimeBodyPart();
 128   
 
 129  0
             textbody.setContent(baos.toString(), message.getMimeType());
 130  0
             attachments.addBodyPart(textbody);
 131   
 
 132  0
             Enumeration e = files.elements();
 133   
 
 134  0
             while (e.hasMoreElements()) {
 135  0
                 File file = (File) e.nextElement();
 136   
 
 137  0
                 MimeBodyPart body;
 138   
 
 139  0
                 body = new MimeBodyPart();
 140  0
                 if (!file.exists() || !file.canRead()) {
 141  0
                     throw new BuildException("File \"" + file.getAbsolutePath()
 142   
                          + "\" does not exist or is not "
 143   
                          + "readable.");
 144   
                 }
 145  0
                 FileDataSource fileData = new FileDataSource(file);
 146  0
                 DataHandler fileDataHandler = new DataHandler(fileData);
 147   
 
 148  0
                 body.setDataHandler(fileDataHandler);
 149  0
                 body.setFileName(file.getName());
 150  0
                 attachments.addBodyPart(body);
 151   
             }
 152   
 
 153  0
             msg.setContent(attachments);
 154  0
             Transport.send(msg);
 155   
         } catch (MessagingException e) {
 156  0
             throw new BuildException("Problem while sending mime mail:", e);
 157   
         } catch (IOException e) {
 158  0
             throw new BuildException("Problem while sending mime mail:", e);
 159   
         }
 160   
     }
 161   
 
 162   
 
 163  0
     private static InternetAddress[] internetAddresses(Vector list)
 164   
          throws AddressException, UnsupportedEncodingException {
 165  0
         InternetAddress[] addrs = new InternetAddress[list.size()];
 166   
 
 167  0
         for (int i = 0; i < list.size(); ++i) {
 168  0
             EmailAddress addr = (EmailAddress) list.elementAt(i);
 169   
 
 170  0
             if (addr.getName() == null) {
 171  0
                 addrs[i] = new InternetAddress(addr.getAddress());
 172   
             } else {
 173  0
                 addrs[i] = new InternetAddress(addr.getAddress(),
 174   
                     addr.getName());
 175   
             }
 176   
         }
 177   
 
 178  0
         return addrs;
 179   
     }
 180   
 }
 181   
 
 182