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.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  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          if ( StringUtils.isEmpty( getSmtpHost() ) )
65          {
66              System.out.println( "Error in configuration: Missing smtpHost." );
67          }
68  
69          if ( getSmtpPort() == 0 )
70          {
71              setSmtpPort( DEFAULT_SMTP_PORT );
72          }
73  
74          props = new Properties();
75  
76          props.put( "mail.smtp.host", getSmtpHost() );
77  
78          props.put( "mail.smtp.port", String.valueOf( getSmtpPort() ) );
79  
80          if ( getUsername() != null )
81          {
82              props.put( "mail.smtp.auth", "true" );
83          }
84  
85          props.put( "mail.debug", String.valueOf( getLogger().isDebugEnabled() ) );
86  
87          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                  this.getClass().getClassLoader().loadClass( "com.sun.net.ssl.internal.ssl.Provider" );
93  
94                  Security.addProvider( new com.sun.net.ssl.internal.ssl.Provider() );
95  
96                  props.put( "mail.smtp.socketFactory.port", String.valueOf( getSmtpPort() ) );
97  
98                  props.put( "mail.smtp.socketFactory.class", SSL_FACTORY );
99  
100                 props.put( "mail.smtp.socketFactory.fallback", "false" );
101             }
102             catch ( ClassNotFoundException e )
103             {
104                 getLogger().error( "You can't use sslMode because your system is missing an SSL Provider.", e );
105             }
106         }
107         if ( userProperties != null )
108         {
109             for ( Iterator i = userProperties.keySet().iterator(); i.hasNext(); )
110             {
111                 String key = (String) i.next();
112 
113                 String value = userProperties.getProperty( key );
114 
115                 props.put( key, value );
116             }
117         }
118     }
119 
120     // ----------------------------------------------------------------------
121     // MailSender Implementation
122     // ----------------------------------------------------------------------
123 
124     public void send( MailMessage mail )
125         throws MailSenderException
126     {
127         verify( mail );
128 
129         try
130         {
131             Authenticator auth = null;
132 
133             if ( getUsername() != null )
134             {
135                 auth = new Authenticator()
136                 {
137                     protected PasswordAuthentication getPasswordAuthentication()
138                     {
139                         return new PasswordAuthentication( getUsername(), getPassword() );
140                     }
141                 };
142             }
143 
144             Session session = Session.getDefaultInstance( props, auth );
145 
146             session.setDebug( getLogger().isDebugEnabled() );
147 
148             Message msg = new MimeMessage( session );
149             InternetAddress addressFrom = new InternetAddress( mail.getFrom().getRfc2822Address() );
150             msg.setFrom( addressFrom );
151 
152             if ( mail.getToAddresses().size() > 0 )
153             {
154                 InternetAddress[] addressTo = new InternetAddress[mail.getToAddresses().size()];
155                 int count = 0;
156                 for ( Iterator i = mail.getToAddresses().iterator(); i.hasNext(); )
157                 {
158                     String address = ( (MailMessage.Address) i.next() ).getRfc2822Address();
159                     addressTo[count++] = new InternetAddress( address );
160                 }
161                 msg.setRecipients( Message.RecipientType.TO, addressTo );
162             }
163 
164             if ( mail.getCcAddresses().size() > 0 )
165             {
166                 InternetAddress[] addressCc = new InternetAddress[mail.getCcAddresses().size()];
167                 int count = 0;
168                 for ( Iterator i = mail.getCcAddresses().iterator(); i.hasNext(); )
169                 {
170                     String address = ( (MailMessage.Address) i.next() ).getRfc2822Address();
171                     addressCc[count++] = new InternetAddress( address );
172                 }
173                 msg.setRecipients( Message.RecipientType.CC, addressCc );
174             }
175 
176             if ( mail.getBccAddresses().size() > 0 )
177             {
178                 InternetAddress[] addressBcc = new InternetAddress[mail.getBccAddresses().size()];
179                 int count = 0;
180                 for ( Iterator i = mail.getBccAddresses().iterator(); i.hasNext(); )
181                 {
182                     String address = ( (MailMessage.Address) i.next() ).getRfc2822Address();
183                     addressBcc[count++] = new InternetAddress( address );
184                 }
185                 msg.setRecipients( Message.RecipientType.BCC, addressBcc );
186             }
187 
188             // Setting the Subject and Content Type
189             msg.setSubject( mail.getSubject() );
190             msg.setContent( mail.getContent(), mail.getContentType() == null ? "text/plain" : mail.getContentType() );
191 
192             if ( mail.getSendDate() != null )
193             {
194                 msg.setHeader( "Date", DateFormatUtils.getDateHeader( mail.getSendDate() ) );
195             }
196             else
197             {
198                 msg.setHeader( "Date", DateFormatUtils.getDateHeader( new Date() ) );
199             }
200 
201             // Send the message
202             Transport.send( msg );
203         }
204         catch ( MessagingException e )
205         {
206             throw new MailSenderException( "Error while sending mail.", e );
207         }
208     }
209 }