View Javadoc
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.Properties;
25  
26  import javax.mail.Authenticator;
27  import javax.mail.Message;
28  import javax.mail.MessagingException;
29  import javax.mail.PasswordAuthentication;
30  import javax.mail.Session;
31  import javax.mail.Transport;
32  import javax.mail.internet.InternetAddress;
33  import javax.mail.internet.MimeMessage;
34  
35  import org.codehaus.plexus.mailsender.AbstractMailSender;
36  import org.codehaus.plexus.mailsender.MailMessage;
37  import org.codehaus.plexus.mailsender.MailSenderException;
38  import org.codehaus.plexus.mailsender.util.DateFormatUtils;
39  import org.codehaus.plexus.util.StringUtils;
40  
41  /**
42   * Helper class for sending email.
43   */
44  public class ProjectJavamailMailSender
45      extends AbstractMailSender
46  {
47      private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
48  
49      // ----------------------------------------------------------------------
50      //
51      // ----------------------------------------------------------------------
52  
53      private Properties userProperties;
54  
55      private Properties props;
56  
57      // ----------------------------------------------------------------------
58      // Component Lifecycle
59      // ----------------------------------------------------------------------
60  
61      public void initialize()
62      {
63          if ( StringUtils.isEmpty( getSmtpHost() ) )
64          {
65              System.out.println( "Error in configuration: Missing smtpHost." );
66          }
67  
68          if ( getSmtpPort() == 0 )
69          {
70              setSmtpPort( DEFAULT_SMTP_PORT );
71          }
72  
73          props = new Properties();
74  
75          props.put( "mail.smtp.host", getSmtpHost() );
76  
77          props.put( "mail.smtp.port", String.valueOf( getSmtpPort() ) );
78  
79          if ( getUsername() != null )
80          {
81              props.put( "mail.smtp.auth", "true" );
82          }
83  
84          props.put( "mail.debug", String.valueOf( getLogger().isDebugEnabled() ) );
85  
86          if ( isSslMode() )
87          {
88              try
89              {
90                  // Try to load the SSL Provider class before we use it, it isn't present in non-Sun JVMs
91                  this.getClass().getClassLoader().loadClass( "com.sun.net.ssl.internal.ssl.Provider" );
92  
93                  Security.addProvider( new com.sun.net.ssl.internal.ssl.Provider() );
94  
95                  props.put( "mail.smtp.socketFactory.port", String.valueOf( getSmtpPort() ) );
96  
97                  props.put( "mail.smtp.socketFactory.class", SSL_FACTORY );
98  
99                  props.put( "mail.smtp.socketFactory.fallback", "false" );
100             }
101             catch ( ClassNotFoundException e )
102             {
103                 getLogger().error( "You can't use sslMode because your system is missing an SSL Provider.", e );
104             }
105         }
106 
107         if ( isTlsEnabled() )
108         {
109             props.put( "mail.smtp.starttls.enable", "true" );
110         }
111 
112         if ( userProperties != null )
113         {
114             for ( Object o : userProperties.keySet() )
115             {
116                 String key = (String) o;
117 
118                 String value = userProperties.getProperty( key );
119 
120                 props.put( key, value );
121             }
122         }
123     }
124 
125     // ----------------------------------------------------------------------
126     // MailSender Implementation
127     // ----------------------------------------------------------------------
128 
129     public void send( MailMessage mail )
130         throws MailSenderException
131     {
132         verify( mail );
133 
134         try
135         {
136             Authenticator auth = null;
137 
138             if ( getUsername() != null )
139             {
140                 auth = new Authenticator()
141                 {
142                     protected PasswordAuthentication getPasswordAuthentication()
143                     {
144                         return new PasswordAuthentication( getUsername(), getPassword() );
145                     }
146                 };
147             }
148 
149             Session session = Session.getDefaultInstance( props, auth );
150 
151             session.setDebug( getLogger().isDebugEnabled() );
152 
153             Message msg = new MimeMessage( session );
154             InternetAddress addressFrom = new InternetAddress( mail.getFrom().getRfc2822Address() );
155             msg.setFrom( addressFrom );
156 
157             if ( mail.getToAddresses().size() > 0 )
158             {
159                 InternetAddress[] addressTo = new InternetAddress[mail.getToAddresses().size()];
160                 int count = 0;
161                 for ( Object o : mail.getToAddresses() )
162                 {
163                     String address = ( (MailMessage.Address) o ).getRfc2822Address();
164                     addressTo[count++] = new InternetAddress( address );
165                 }
166                 msg.setRecipients( Message.RecipientType.TO, addressTo );
167             }
168 
169             if ( mail.getCcAddresses().size() > 0 )
170             {
171                 InternetAddress[] addressCc = new InternetAddress[mail.getCcAddresses().size()];
172                 int count = 0;
173                 for ( Object o : mail.getCcAddresses() )
174                 {
175                     String address = ( (MailMessage.Address) o ).getRfc2822Address();
176                     addressCc[count++] = new InternetAddress( address );
177                 }
178                 msg.setRecipients( Message.RecipientType.CC, addressCc );
179             }
180 
181             if ( mail.getBccAddresses().size() > 0 )
182             {
183                 InternetAddress[] addressBcc = new InternetAddress[mail.getBccAddresses().size()];
184                 int count = 0;
185                 for ( Object o : mail.getBccAddresses() )
186                 {
187                     String address = ( (MailMessage.Address ) o ).getRfc2822Address();
188                     addressBcc[count++] = new InternetAddress( address );
189                 }
190                 msg.setRecipients( Message.RecipientType.BCC, addressBcc );
191             }
192 
193             // Setting the Subject and Content Type
194             msg.setSubject( mail.getSubject() );
195             msg.setContent( mail.getContent(), mail.getContentType() == null ? "text/plain" : mail.getContentType() );
196 
197             if ( mail.getSendDate() != null )
198             {
199                 msg.setHeader( "Date", DateFormatUtils.getDateHeader( mail.getSendDate() ) );
200             }
201             else
202             {
203                 msg.setHeader( "Date", DateFormatUtils.getDateHeader( new Date() ) );
204             }
205 
206             // Send the message
207             Transport.send( msg );
208         }
209         catch ( MessagingException e )
210         {
211             throw new MailSenderException( "Error while sending mail.", e );
212         }
213     }
214 }