Coverage Report - org.apache.maven.plugin.announcement.mailsender.ProjectJavamailMailSender
 
Classes in this File Line Coverage Branch Coverage Complexity
ProjectJavamailMailSender
0%
0/70
0%
0/30
7
ProjectJavamailMailSender$1
0%
0/2
N/A
7
 
 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  
             try
 90  
             {
 91  
                 // Try to load the SSL Provider class before we use it, it isn't present in non-Sun JVMs
 92  0
                 this.getClass().getClassLoader().loadClass( "com.sun.net.ssl.internal.ssl.Provider" );
 93  
 
 94  0
                 Security.addProvider( new com.sun.net.ssl.internal.ssl.Provider() );
 95  
 
 96  0
                 props.put( "mail.smtp.socketFactory.port", String.valueOf( getSmtpPort() ) );
 97  
 
 98  0
                 props.put( "mail.smtp.socketFactory.class", SSL_FACTORY );
 99  
 
 100  0
                 props.put( "mail.smtp.socketFactory.fallback", "false" );
 101  
             }
 102  0
             catch ( ClassNotFoundException e )
 103  
             {
 104  0
                 getLogger().error( "You can't use sslMode because your system is missing an SSL Provider.", e );
 105  0
             }
 106  
         }
 107  0
         if ( userProperties != null )
 108  
         {
 109  0
             for ( Iterator i = userProperties.keySet().iterator(); i.hasNext(); )
 110  
             {
 111  0
                 String key = (String) i.next();
 112  
 
 113  0
                 String value = userProperties.getProperty( key );
 114  
 
 115  0
                 props.put( key, value );
 116  0
             }
 117  
         }
 118  0
     }
 119  
 
 120  
     // ----------------------------------------------------------------------
 121  
     // MailSender Implementation
 122  
     // ----------------------------------------------------------------------
 123  
 
 124  
     public void send( MailMessage mail )
 125  
         throws MailSenderException
 126  
     {
 127  0
         verify( mail );
 128  
 
 129  
         try
 130  
         {
 131  0
             Authenticator auth = null;
 132  
 
 133  0
             if ( getUsername() != null )
 134  
             {
 135  0
                 auth = new Authenticator()
 136  
                 {
 137  0
                     protected PasswordAuthentication getPasswordAuthentication()
 138  
                     {
 139  0
                         return new PasswordAuthentication( getUsername(), getPassword() );
 140  
                     }
 141  
                 };
 142  
             }
 143  
 
 144  0
             Session session = Session.getDefaultInstance( props, auth );
 145  
 
 146  0
             session.setDebug( getLogger().isDebugEnabled() );
 147  
 
 148  0
             Message msg = new MimeMessage( session );
 149  0
             InternetAddress addressFrom = new InternetAddress( mail.getFrom().getRfc2822Address() );
 150  0
             msg.setFrom( addressFrom );
 151  
 
 152  0
             if ( mail.getToAddresses().size() > 0 )
 153  
             {
 154  0
                 InternetAddress[] addressTo = new InternetAddress[mail.getToAddresses().size()];
 155  0
                 int count = 0;
 156  0
                 for ( Iterator i = mail.getToAddresses().iterator(); i.hasNext(); )
 157  
                 {
 158  0
                     String address = ( (MailMessage.Address) i.next() ).getRfc2822Address();
 159  0
                     addressTo[count++] = new InternetAddress( address );
 160  0
                 }
 161  0
                 msg.setRecipients( Message.RecipientType.TO, addressTo );
 162  
             }
 163  
 
 164  0
             if ( mail.getCcAddresses().size() > 0 )
 165  
             {
 166  0
                 InternetAddress[] addressCc = new InternetAddress[mail.getCcAddresses().size()];
 167  0
                 int count = 0;
 168  0
                 for ( Iterator i = mail.getCcAddresses().iterator(); i.hasNext(); )
 169  
                 {
 170  0
                     String address = ( (MailMessage.Address) i.next() ).getRfc2822Address();
 171  0
                     addressCc[count++] = new InternetAddress( address );
 172  0
                 }
 173  0
                 msg.setRecipients( Message.RecipientType.CC, addressCc );
 174  
             }
 175  
 
 176  0
             if ( mail.getBccAddresses().size() > 0 )
 177  
             {
 178  0
                 InternetAddress[] addressBcc = new InternetAddress[mail.getBccAddresses().size()];
 179  0
                 int count = 0;
 180  0
                 for ( Iterator i = mail.getBccAddresses().iterator(); i.hasNext(); )
 181  
                 {
 182  0
                     String address = ( (MailMessage.Address) i.next() ).getRfc2822Address();
 183  0
                     addressBcc[count++] = new InternetAddress( address );
 184  0
                 }
 185  0
                 msg.setRecipients( Message.RecipientType.BCC, addressBcc );
 186  
             }
 187  
 
 188  
             // Setting the Subject and Content Type
 189  0
             msg.setSubject( mail.getSubject() );
 190  0
             msg.setContent( mail.getContent(), mail.getContentType() == null ? "text/plain" : mail.getContentType() );
 191  
 
 192  0
             if ( mail.getSendDate() != null )
 193  
             {
 194  0
                 msg.setHeader( "Date", DateFormatUtils.getDateHeader( mail.getSendDate() ) );
 195  
             }
 196  
             else
 197  
             {
 198  0
                 msg.setHeader( "Date", DateFormatUtils.getDateHeader( new Date() ) );
 199  
             }
 200  
 
 201  
             // Send the message
 202  0
             Transport.send( msg );
 203  
         }
 204  0
         catch ( MessagingException e )
 205  
         {
 206  0
             throw new MailSenderException( "Error while sending mail.", e );
 207  0
         }
 208  0
     }
 209  
 }