antoine     2003/05/28 13:58:04

  Modified:    src/main/org/apache/tools/ant/listener MailLogger.java
               src/main/org/apache/tools/ant/taskdefs/email Mailer.java
                        MimeMailer.java EmailTask.java
               docs/manual listeners.html
               docs/manual/CoreTasks mail.html
               .        WHATSNEW
  Added:       src/etc/testcases/taskdefs/email mail.xml
               src/testcases/org/apache/tools/ant/taskdefs/email
                        EmailTaskTest.java
  Removed:     src/etc/testcases/taskdefs mail.xml
               src/testcases/org/apache/tools/ant/taskdefs
                        EmailTaskTest.java
  Log:
  Support for SMTP over TLS/SSL in the mail task
  PR: 19180
  
  Revision  Changes    Path
  1.19      +9 -5      
ant/src/main/org/apache/tools/ant/listener/MailLogger.java
  
  Index: MailLogger.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/main/org/apache/tools/ant/listener/MailLogger.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- MailLogger.java   28 May 2003 08:02:40 -0000      1.18
  +++ MailLogger.java   28 May 2003 20:58:02 -0000      1.19
  @@ -157,16 +157,18 @@
               int port = 
Integer.parseInt(getValue(properties,"port",String.valueOf(MailMessage.DEFAULT_PORT)));
               String user = getValue(properties, "user", "");
               String password = getValue(properties, "password", "");
  +            boolean ssl = Project.toBoolean(getValue(properties,
  +                     "ssl", "off"));
               String from = getValue(properties, "from", null);
               String replytoList = getValue(properties,"replyto","");
               String toList = getValue(properties, prefix + ".to", null);
               String subject = getValue(properties, prefix + ".subject",
                       (success) ? "Build Success" : "Build Failure");
  -            if (user.equals("") && password.equals("")) {
  +            if (user.equals("") && password.equals("") && !ssl) {
                   sendMail(mailhost, port,  from, replytoList, toList, 
subject, buffer.substring(0));
               }
               else {
  -                sendMimeMail(event.getProject(), mailhost, port, user, 
password, from, replytoList, toList, subject, buffer.substring(0));
  +                sendMimeMail(event.getProject(), mailhost, port, user, 
password, ssl, from, replytoList, toList, subject, buffer.substring(0));
               }
           } catch (Exception e) {
               System.out.println("MailLogger failed to send e-mail!");
  @@ -256,15 +258,16 @@
        * @param  port             mail server port number
        * @param  user             user name for SMTP auth
        * @param  password         password for SMTP auth
  +     * @param  ssl              if true send message over SSL
        * @param  from             from address
        * @param  replyToString    comma-separated replyto list
        * @param  toString         comma-separated recipient list
        * @param  subject          mail subject
        * @param  message          mail body
  -     * @exception  IOException  thrown if sending message fails
        */
  -    private void sendMimeMail(Project project, String host, int port, String 
user, String password, String from, String replyToString, String toString,
  -                          String subject, String message) throws IOException 
{
  +    private void sendMimeMail(Project project, String host, int port, String 
user, String password, boolean ssl,
  +                              String from, String replyToString, String 
toString,
  +                          String subject, String message)  {
           // convert the replyTo string into a vector of emailaddresses
           Mailer mailer = null;
               try {
  @@ -280,6 +283,7 @@
           mailer.setPort(port);
           mailer.setUser(user);
           mailer.setPassword(password);
  +        mailer.setSSL(ssl);
           Message mymessage = new Message(message);
           mymessage.setProject(project);
           mailer.setMessage(mymessage);
  
  
  
  1.12      +13 -0     
ant/src/main/org/apache/tools/ant/taskdefs/email/Mailer.java
  
  Index: Mailer.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/email/Mailer.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- Mailer.java       28 May 2003 08:02:41 -0000      1.11
  +++ Mailer.java       28 May 2003 20:58:03 -0000      1.12
  @@ -69,6 +69,7 @@
       protected int port = -1;
       protected String user = null;
       protected String password = null;
  +    protected boolean SSL = false;
       protected Message message;
       protected EmailAddress from;
       protected Vector replyToList = null;
  @@ -103,6 +104,7 @@
        * Sets the user for smtp auth
        *
        * @param user
  +     * @since ant 1.6
        */
       public void setUser(String user) {
           this.user = user;
  @@ -112,9 +114,20 @@
        * Sets the password for smtp auth
        *
        * @param password
  +     * @since ant 1.6
        */
       public void setPassword(String password) {
           this.password = password;
  +    }
  +
  +    /**
  +     * Sets whether the user wants to send the mail through SSL
  +     *
  +     * @param SSL
  +     * @since ant 1.6
  +     */
  +    public void setSSL(boolean SSL) {
  +        this.SSL = SSL;
       }
   
       /**
  
  
  
  1.12      +14 -1     
ant/src/main/org/apache/tools/ant/taskdefs/email/MimeMailer.java
  
  Index: MimeMailer.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/email/MimeMailer.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- MimeMailer.java   27 May 2003 15:25:48 -0000      1.11
  +++ MimeMailer.java   28 May 2003 20:58:03 -0000      1.12
  @@ -66,6 +66,7 @@
   import java.util.Properties;
   import java.util.StringTokenizer;
   import java.util.Vector;
  +import java.security.Security;
   
   import javax.activation.DataHandler;
   import javax.activation.FileDataSource;
  @@ -157,6 +158,19 @@
               // alcohol :-)
               Session sesh;
               Authenticator auth;
  +            if (SSL) {
  +                try {
  +                    java.security.Provider 
p=(java.security.Provider)Class.forName( 
"com.sun.net.ssl.internal.ssl.Provider").newInstance();
  +                    Security.addProvider(p);
  +                }
  +                catch (Exception e) {
  +                    throw new BuildException("could not instantiate ssl 
security provider, check that you have JSSE in your classpath");
  +                }
  +                final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
  +                // SMTP provider
  +                props.put( "mail.smtp.socketFactory.class", SSL_FACTORY);
  +                props.put( "mail.smtp.socketFactory.fallback", "false");
  +            }
               if (user==null && password == null) {
                   sesh = Session.getDefaultInstance(props, null);
               }
  @@ -165,7 +179,6 @@
                   auth = new SimpleAuthenticator(user,password);
                   sesh = Session.getInstance(props,auth);
               }
  -
               //create the message
               MimeMessage msg = new MimeMessage(sesh);
               MimeMultipart attachments = new MimeMultipart();
  
  
  
  1.20      +18 -2     
ant/src/main/org/apache/tools/ant/taskdefs/email/EmailTask.java
  
  Index: EmailTask.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/email/EmailTask.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- EmailTask.java    27 May 2003 15:25:48 -0000      1.19
  +++ EmailTask.java    28 May 2003 20:58:03 -0000      1.20
  @@ -108,7 +108,6 @@
           }
       }
   
  -
       private String encoding = AUTO;
       /** host running SMTP  */
       private String host = "localhost";
  @@ -146,6 +145,8 @@
       private String user=null;
       /** Password for SMTP auth */
       private String password=null;
  +    /** indicate if the user wishes SSL-TLS */
  +    private boolean SSL = false;
   
       /**
        * sets the user for SMTP auth; this requires JavaMail
  @@ -166,6 +167,15 @@
       }
   
       /**
  +     * tells if the user needs to send his data over SSL
  +     * @param SSL
  +     * @since ant 1.6
  +     */
  +    public void setSSL(boolean SSL) {
  +        this.SSL = SSL;
  +    }
  +
  +    /**
        * Allows the build writer to choose the preferred encoding method
        *
        * @param encoding The encoding (one of AUTO,MIME,UU,PLAIN)
  @@ -466,9 +476,14 @@
                   }
               }
               // SMTP auth only allowed with MIME mail
  -            if (autoFound==false && ((user !=null) || (password != null)) && 
(encoding.equals(UU) || encoding.equals(PLAIN))) {
  +            if (autoFound==false && ((user !=null) || (password != null) ) 
&& (encoding.equals(UU) || encoding.equals(PLAIN))) {
                   throw new BuildException("SMTP auth only possible with MIME 
mail");
               }
  +            // SSL only allowed with MIME mail
  +            if (autoFound==false && (SSL) && (encoding.equals(UU) || 
encoding.equals(PLAIN))) {
  +                throw new BuildException("SSL only possible with MIME mail");
  +            }
  +
   
               // try UU format
               if (encoding.equals(UU)
  @@ -564,6 +579,7 @@
               mailer.setPort(port);
               mailer.setUser(user);
               mailer.setPassword(password);
  +            mailer.setSSL(SSL);
               mailer.setMessage(message);
               mailer.setFrom(from);
               mailer.setReplyToList(replyToList);
  
  
  
  1.1                  ant/src/etc/testcases/taskdefs/email/mail.xml
  
  Index: mail.xml
  ===================================================================
  <?xml version="1.0"?>
  
  <project name="mail-test" basedir="." default="test1">
  
    <target name="test1">
      <!-- this test is supposed to bring a build exception because user and 
password is not allowed with plain encoding -->
      <mail host="localhost" port="25" from="[EMAIL PROTECTED]" to="[EMAIL 
PROTECTED]" subject="hello" encoding="plain" user="joe" password="secret">
          <message>
              Hi Laura, how are you doing ?
          </message>
      </mail>
    </target>
    <target name="test2">
        <!-- this test is supposed to bring a build exception because SSL is 
not allowed with plain encoding -->
        <mail host="localhost" port="465" from="[EMAIL PROTECTED]" to="[EMAIL 
PROTECTED]" subject="hello" encoding="plain" ssl="true">
            <message>
                Hi Laura, how are you doing ?
            </message>
        </mail>
      </target>
  
  
  
  </project>
  
  
  
  1.1                  
ant/src/testcases/org/apache/tools/ant/taskdefs/email/EmailTaskTest.java
  
  Index: EmailTaskTest.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "Ant" and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.tools.ant.taskdefs.email;
  
  import org.apache.tools.ant.BuildFileTest;
  
  /**
   * TODO : develop these testcases - the email task needs to have attributes 
allowing
   * to simulate sending mail and to catch the output in text files or streams
   * @author <a href="mailto:[EMAIL PROTECTED]">Antoine Levy-Lambert</a>
   */
  public class EmailTaskTest extends BuildFileTest {
      
      public EmailTaskTest(String name) {
          super(name);
      }    
      
      public void setUp() { 
          configureProject("src/etc/testcases/taskdefs/email/mail.xml");
      }
  
      public void test1() { 
          expectBuildException("test1", "SMTP auth only possible with MIME 
mail");
      }
  
      public void test2() {
          expectBuildException("test2", "SSL only possible with MIME mail");
      }
  
  }
  
  
  
  1.14      +7 -0      ant/docs/manual/listeners.html
  
  Index: listeners.html
  ===================================================================
  RCS file: /home/cvs/ant/docs/manual/listeners.html,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- listeners.html    27 May 2003 15:33:25 -0000      1.13
  +++ listeners.html    28 May 2003 20:58:03 -0000      1.14
  @@ -138,6 +138,13 @@
       the email message will be then sent using Mime and requires JavaMail</td>
     </tr>
     <tr>
  +    <td width="337">MailLogger.ssl</td>
  +    <td width="63%">on or true if ssl is needed<br/>
  +    This feature requires JavaMail</td>
  +    <td width="63%">
  +    no</td>
  +  </tr>
  +  <tr>
       <td width="337">MailLogger.from</td>
       <td width="63%">Mail &quot;from&quot; address</td>
       <td width="63%">Yes, if mail needs to be sent</td>
  
  
  
  1.20      +7 -1      ant/docs/manual/CoreTasks/mail.html
  
  Index: mail.html
  ===================================================================
  RCS file: /home/cvs/ant/docs/manual/CoreTasks/mail.html,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- mail.html 27 May 2003 15:33:25 -0000      1.19
  +++ mail.html 28 May 2003 20:58:03 -0000      1.20
  @@ -13,7 +13,7 @@
   This task can send mail using either plain
   text, UU encoding, or MIME format mail, depending on what is available.<br/>
   <br/>
  -If you need SMTP auth, you have to use MIME (and therefore to install 
JavaMail).<br/><br/>
  +SMTP auth and SSL/TLS require JavaMail and are only available in MIME 
format.<br/><br/>
   Attachments may be sent using nested
   <a href="../CoreTypes/fileset.html">fileset</a> elements.</p>
   <p><strong>Note:</strong> This task may depend on external libraries
  @@ -112,6 +112,12 @@
       <td valign="top">password for SMTP auth</td>
       <td valign="center">Yes, if SMTP auth is required on your SMTP 
server<br/>
       the email message will be then sent using Mime and requires JavaMail</td>
  +  </tr>
  +  <tr>
  +    <td valign="top">ssl</td>
  +    <td valign="top">"true", "on" or "yes" accepted here<br/>
  +    indicates whether you need TLS/SSL</td>
  +    <td valign="center">No</td>
     </tr>
     <tr>
       <td valign="top">encoding</td>
  
  
  
  1.431     +3 -0      ant/WHATSNEW
  
  Index: WHATSNEW
  ===================================================================
  RCS file: /home/cvs/ant/WHATSNEW,v
  retrieving revision 1.430
  retrieving revision 1.431
  diff -u -r1.430 -r1.431
  --- WHATSNEW  28 May 2003 13:12:02 -0000      1.430
  +++ WHATSNEW  28 May 2003 20:58:03 -0000      1.431
  @@ -307,6 +307,9 @@
   Implementation with plain mail remains to do.
   Bugzilla Report 5969.
   
  +* <mail> and mailloger support SMTP over TLS/SSL
  +Bugzilla Report 19180.
  +
   * <zipfileset> can now be defined in the main body of a project
     and referred to with refid="xyz". Bugzilla Report 17007.
   
  
  
  

Reply via email to