Coverage Report - org.apache.maven.plugin.announcement.mailsender.ProjectJavamailMailSender
 
Classes in this File Line Coverage Branch Coverage Complexity
ProjectJavamailMailSender
0%
0/62
0%
0/30
6,667
ProjectJavamailMailSender$1
0%
0/2
N/A
6,667
 
 1  
 package org.apache.maven.plugin.announcement.mailsender;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.security.Security;
 23  
 import java.util.Date;
 24  
 import java.util.Iterator;
 25  
 import java.util.Properties;
 26  
 
 27  
 import javax.mail.Authenticator;
 28  
 import javax.mail.Message;
 29  
 import javax.mail.MessagingException;
 30  
 import javax.mail.PasswordAuthentication;
 31  
 import javax.mail.Session;
 32  
 import javax.mail.Transport;
 33  
 import javax.mail.internet.InternetAddress;
 34  
 import javax.mail.internet.MimeMessage;
 35  
 
 36  
 import org.codehaus.plexus.mailsender.AbstractMailSender;
 37  
 import org.codehaus.plexus.mailsender.MailMessage;
 38  
 import org.codehaus.plexus.mailsender.MailSenderException;
 39  
 import org.codehaus.plexus.mailsender.util.DateFormatUtils;
 40  
 import org.codehaus.plexus.util.StringUtils;
 41  
 
 42  
 /**
 43  
  * Helper class for sending email.
 44  
  */
 45  0
 public class ProjectJavamailMailSender
 46  
     extends AbstractMailSender
 47  
 {
 48  
     private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
 49  
 
 50  
     // ----------------------------------------------------------------------
 51  
     //
 52  
     // ----------------------------------------------------------------------
 53  
 
 54  
     private Properties userProperties;
 55  
 
 56  
     private Properties props;
 57  
 
 58  
     // ----------------------------------------------------------------------
 59  
     // Component Lifecycle
 60  
     // ----------------------------------------------------------------------
 61  
 
 62  
     public void initialize()
 63  
     {
 64  0
         if ( StringUtils.isEmpty( getSmtpHost() ) )
 65  
         {
 66  0
             System.out.println( "Error in configuration: Missing smtpHost." );
 67  
         }
 68  
 
 69  0
         if ( getSmtpPort() == 0 )
 70  
         {
 71  0
             setSmtpPort( DEFAULT_SMTP_PORT );
 72  
         }
 73  
 
 74  0
         props = new Properties();
 75  
 
 76  0
         props.put( "mail.smtp.host", getSmtpHost() );
 77  
 
 78  0
         props.put( "mail.smtp.port", String.valueOf( getSmtpPort() ) );
 79  
 
 80  0
         if ( getUsername() != null )
 81  
         {
 82  0
             props.put( "mail.smtp.auth", "true" );
 83  
         }
 84  
 
 85  0
         props.put( "mail.debug", String.valueOf( getLogger().isDebugEnabled() ) );
 86  
 
 87  0
         if ( isSslMode() )
 88  
         {
 89  0
             Security.addProvider( new com.sun.net.ssl.internal.ssl.Provider() );
 90  
 
 91  0
             props.put( "mail.smtp.socketFactory.port", String.valueOf( getSmtpPort() ) );
 92  
 
 93  0
             props.put( "mail.smtp.socketFactory.class", SSL_FACTORY );
 94  
 
 95  0
             props.put( "mail.smtp.socketFactory.fallback", "false" );
 96  
         }
 97  
 
 98  0
         if ( userProperties != null )
 99  
         {
 100  0
             for ( Iterator i = userProperties.keySet().iterator(); i.hasNext(); )
 101  
             {
 102  0
                 String key = (String) i.next();
 103  
 
 104  0
                 String value = userProperties.getProperty( key );
 105  
 
 106  0
                 props.put( key, value );
 107  
             }
 108  
         }
 109  0
     }
 110  
 
 111  
     // ----------------------------------------------------------------------
 112  
     // MailSender Implementation
 113  
     // ----------------------------------------------------------------------
 114  
 
 115  
     public void send( MailMessage mail )
 116  
         throws MailSenderException
 117  
     {
 118  0
         verify( mail );
 119  
 
 120  
         try
 121  
         {
 122  0
             Authenticator auth = null;
 123  
 
 124  0
             if ( getUsername() != null )
 125  
             {
 126  0
                 auth = new Authenticator()
 127  
                 {
 128  0
                     protected PasswordAuthentication getPasswordAuthentication()
 129  
                     {
 130  0
                         return new PasswordAuthentication( getUsername(), getPassword() );
 131  
                     }
 132  
                 };
 133  
             }
 134  
 
 135  0
             Session session = Session.getDefaultInstance( props, auth );
 136  
 
 137  0
             session.setDebug( getLogger().isDebugEnabled() );
 138  
 
 139  0
             Message msg = new MimeMessage( session );
 140  0
             InternetAddress addressFrom = new InternetAddress( mail.getFrom().getRfc2822Address() );
 141  0
             msg.setFrom( addressFrom );
 142  
 
 143  0
             if ( mail.getToAddresses().size() > 0 )
 144  
             {
 145  0
                 InternetAddress[] addressTo = new InternetAddress[mail.getToAddresses().size()];
 146  0
                 int count = 0;
 147  0
                 for ( Iterator i = mail.getToAddresses().iterator(); i.hasNext(); )
 148  
                 {
 149  0
                     String address = ( (MailMessage.Address) i.next() ).getRfc2822Address();
 150  0
                     addressTo[count++] = new InternetAddress( address );
 151  
                 }
 152  0
                 msg.setRecipients( Message.RecipientType.TO, addressTo );
 153  
             }
 154  
 
 155  0
             if ( mail.getCcAddresses().size() > 0 )
 156  
             {
 157  0
                 InternetAddress[] addressCc = new InternetAddress[mail.getCcAddresses().size()];
 158  0
                 int count = 0;
 159  0
                 for ( Iterator i = mail.getCcAddresses().iterator(); i.hasNext(); )
 160  
                 {
 161  0
                     String address = ( (MailMessage.Address) i.next() ).getRfc2822Address();
 162  0
                     addressCc[count++] = new InternetAddress( address );
 163  
                 }
 164  0
                 msg.setRecipients( Message.RecipientType.CC, addressCc );
 165  
             }
 166  
 
 167  0
             if ( mail.getBccAddresses().size() > 0 )
 168  
             {
 169  0
                 InternetAddress[] addressBcc = new InternetAddress[mail.getBccAddresses().size()];
 170  0
                 int count = 0;
 171  0
                 for ( Iterator i = mail.getBccAddresses().iterator(); i.hasNext(); )
 172  
                 {
 173  0
                     String address = ( (MailMessage.Address) i.next() ).getRfc2822Address();
 174  0
                     addressBcc[count++] = new InternetAddress( address );
 175  
                 }
 176  0
                 msg.setRecipients( Message.RecipientType.BCC, addressBcc );
 177  
             }
 178  
 
 179  
             // Setting the Subject and Content Type
 180  0
             msg.setSubject( mail.getSubject() );
 181  0
             msg.setContent( mail.getContent(), mail.getContentType() == null ? "text/plain" : mail.getContentType() );
 182  
 
 183  0
             if ( mail.getSendDate() != null )
 184  
             {
 185  0
                 msg.setHeader( "Date", DateFormatUtils.getDateHeader( mail.getSendDate() ) );
 186  
             }
 187  
             else
 188  
             {
 189  0
                 msg.setHeader( "Date", DateFormatUtils.getDateHeader( new Date() ) );
 190  
             }
 191  
 
 192  
             // Send the message
 193  0
             Transport.send( msg );
 194  
         }
 195  0
         catch ( MessagingException e )
 196  
         {
 197  0
             throw new MailSenderException( "Error while sending mail.", e );
 198  0
         }
 199  0
     }
 200  
 }