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