From 4bcc44728551a0152f09f0fdf23553029e7bbde1 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Tue, 8 Jan 2002 10:50:43 +0000 Subject: [PATCH] Help people who's mail server doesn't speak SMTP correctly. PR: 5273 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270624 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/taskdefs/SendEmail.java | 10 +-- .../tools/mail/ErrorInQuitException.java | 72 +++++++++++++++++++ .../org/apache/tools/mail/MailMessage.java | 59 +++++++++------ 3 files changed, 115 insertions(+), 26 deletions(-) create mode 100644 src/main/org/apache/tools/mail/ErrorInQuitException.java diff --git a/src/main/org/apache/tools/ant/taskdefs/SendEmail.java b/src/main/org/apache/tools/ant/taskdefs/SendEmail.java index e62320fb8..d9b0860a3 100644 --- a/src/main/org/apache/tools/ant/taskdefs/SendEmail.java +++ b/src/main/org/apache/tools/ant/taskdefs/SendEmail.java @@ -62,6 +62,7 @@ import java.io.IOException; import java.util.Vector; import java.util.StringTokenizer; import java.util.Enumeration; +import org.apache.tools.mail.ErrorInQuitException; import org.apache.tools.mail.MailMessage; import org.apache.tools.ant.Task; @@ -305,13 +306,12 @@ public class SendEmail extends Task { log("Sending email"); mailMessage.sendAndClose(); } 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); } - else { - log(err,Project.MSG_ERR); - } } } diff --git a/src/main/org/apache/tools/mail/ErrorInQuitException.java b/src/main/org/apache/tools/mail/ErrorInQuitException.java new file mode 100644 index 000000000..352732b7e --- /dev/null +++ b/src/main/org/apache/tools/mail/ErrorInQuitException.java @@ -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. + * + *

This seems to happen with some version of MS Exchange that + * doesn't respond with a 221 code immediately. See Bug + * report 5273.

+ * + * @author Stefan Bodewig + * @version $Revision$ + */ +public class ErrorInQuitException extends IOException { + + public ErrorInQuitException(IOException e) { + super(e.getMessage()); + } + +} diff --git a/src/main/org/apache/tools/mail/MailMessage.java b/src/main/org/apache/tools/mail/MailMessage.java index 2aaf00ce7..68cc3c2dc 100644 --- a/src/main/org/apache/tools/mail/MailMessage.java +++ b/src/main/org/apache/tools/mail/MailMessage.java @@ -310,9 +310,12 @@ public class MailMessage { * @exception IOException if there's any problem reported by the mail server */ public void sendAndClose() throws IOException { - sendDot(); - sendQuit(); - disconnect(); + try { + sendDot(); + sendQuit(); + } finally { + disconnect(); + } } // 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 } - 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 . - //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 . + String response = in.getResponse(); + if (!isResponseOK(response, ok)) { + throw new IOException("Unexpected reply to command: " + + msg + ": " + response); + } } - } boolean isResponseOK(String response, int[] ok) { // Check that the response is one of the valid codes @@ -424,11 +429,23 @@ public class MailMessage { 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 . becomes ..