PR: 5273 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270624 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -62,6 +62,7 @@ import java.io.IOException; | |||||
| import java.util.Vector; | import java.util.Vector; | ||||
| import java.util.StringTokenizer; | import java.util.StringTokenizer; | ||||
| import java.util.Enumeration; | import java.util.Enumeration; | ||||
| import org.apache.tools.mail.ErrorInQuitException; | |||||
| import org.apache.tools.mail.MailMessage; | import org.apache.tools.mail.MailMessage; | ||||
| import org.apache.tools.ant.Task; | import org.apache.tools.ant.Task; | ||||
| @@ -305,13 +306,12 @@ public class SendEmail extends Task { | |||||
| log("Sending email"); | log("Sending email"); | ||||
| mailMessage.sendAndClose(); | mailMessage.sendAndClose(); | ||||
| } catch (IOException ioe) { | } catch (IOException ioe) { | ||||
| String err="IO error sending mail "+ioe.toString(); | |||||
| if(failOnError) { | |||||
| String err="IO error sending mail: "+ioe.toString(); | |||||
| if (!failOnError || ioe instanceof ErrorInQuitException) { | |||||
| log(err, Project.MSG_ERR); | |||||
| } else { | |||||
| throw new BuildException(err,ioe,location); | throw new BuildException(err,ioe,location); | ||||
| } | } | ||||
| else { | |||||
| log(err,Project.MSG_ERR); | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -0,0 +1,72 @@ | |||||
| /* | |||||
| * The Apache Software License, Version 1.1 | |||||
| * | |||||
| * Copyright (c) 2002 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 "The Jakarta Project", "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 apache@apache.org. | |||||
| * | |||||
| * 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. | |||||
| * ==================================================================== | |||||
| * | |||||
| */ | |||||
| package org.apache.tools.mail; | |||||
| import java.io.IOException; | |||||
| /** | |||||
| * Specialized IOException that get thrown if SMPT's QUIT command fails. | |||||
| * | |||||
| * <p>This seems to happen with some version of MS Exchange that | |||||
| * doesn't respond with a 221 code immediately. See <a | |||||
| * href="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=5273">Bug | |||||
| * report 5273</a>.</p> | |||||
| * | |||||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
| * @version $Revision$ | |||||
| */ | |||||
| public class ErrorInQuitException extends IOException { | |||||
| public ErrorInQuitException(IOException e) { | |||||
| super(e.getMessage()); | |||||
| } | |||||
| } | |||||
| @@ -310,9 +310,12 @@ public class MailMessage { | |||||
| * @exception IOException if there's any problem reported by the mail server | * @exception IOException if there's any problem reported by the mail server | ||||
| */ | */ | ||||
| public void sendAndClose() throws IOException { | public void sendAndClose() throws IOException { | ||||
| sendDot(); | |||||
| sendQuit(); | |||||
| disconnect(); | |||||
| try { | |||||
| sendDot(); | |||||
| sendQuit(); | |||||
| } finally { | |||||
| disconnect(); | |||||
| } | |||||
| } | } | ||||
| // Make a limited attempt to extract a sanitized email address | // Make a limited attempt to extract a sanitized email address | ||||
| @@ -398,21 +401,23 @@ public class MailMessage { | |||||
| send("\r\n.", ok); // make sure dot is on new line | send("\r\n.", ok); // make sure dot is on new line | ||||
| } | } | ||||
| void sendQuit() throws IOException { | |||||
| int[] ok = { 221 }; | |||||
| send("QUIT", ok); | |||||
| } | |||||
| void sendQuit() throws IOException { | |||||
| int[] ok = { 221 }; | |||||
| try { | |||||
| send("QUIT", ok); | |||||
| } catch (IOException e) { | |||||
| throw new ErrorInQuitException(e); | |||||
| } | |||||
| } | |||||
| void send(String msg, int[] ok) throws IOException { | |||||
| out.rawPrint(msg + "\r\n"); // raw supports <CRLF>.<CRLF> | |||||
| //System.out.println("S: " + msg); | |||||
| String response = in.getResponse(); | |||||
| //System.out.println("R: " + response); | |||||
| if (!isResponseOK(response, ok)) { | |||||
| throw new IOException( | |||||
| "Unexpected reply to command: " + msg + ": " + response); | |||||
| void send(String msg, int[] ok) throws IOException { | |||||
| out.rawPrint(msg + "\r\n"); // raw supports <CRLF>.<CRLF> | |||||
| String response = in.getResponse(); | |||||
| if (!isResponseOK(response, ok)) { | |||||
| throw new IOException("Unexpected reply to command: " | |||||
| + msg + ": " + response); | |||||
| } | |||||
| } | } | ||||
| } | |||||
| boolean isResponseOK(String response, int[] ok) { | boolean isResponseOK(String response, int[] ok) { | ||||
| // Check that the response is one of the valid codes | // Check that the response is one of the valid codes | ||||
| @@ -424,11 +429,23 @@ public class MailMessage { | |||||
| return false; | return false; | ||||
| } | } | ||||
| void disconnect() throws IOException { | |||||
| if (out != null) out.close(); | |||||
| if (in != null) in.close(); | |||||
| if (socket != null) socket.close(); | |||||
| } | |||||
| void disconnect() throws IOException { | |||||
| if (out != null) { | |||||
| out.close(); | |||||
| } | |||||
| if (in != null) { | |||||
| try { | |||||
| in.close(); | |||||
| } catch (IOException e) { | |||||
| } | |||||
| } | |||||
| if (socket != null) { | |||||
| try { | |||||
| socket.close(); | |||||
| } catch (IOException e) { | |||||
| } | |||||
| } | |||||
| } | |||||
| } | } | ||||
| // This PrintStream subclass makes sure that <CRLF>. becomes <CRLF>.. | // This PrintStream subclass makes sure that <CRLF>. becomes <CRLF>.. | ||||