ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r704528 - in /ant/core/trunk: ./ docs/manual/CoreTasks/ src/main/org/apache/tools/ant/taskdefs/email/
Date Tue, 14 Oct 2008 13:12:24 GMT
Author: bodewig
Date: Tue Oct 14 06:12:23 2008
New Revision: 704528

URL: http://svn.apache.org/viewvc?rev=704528&view=rev
Log:
Allow <mail> to ignore bad recipients.  PR 36446.

Modified:
    ant/core/trunk/WHATSNEW
    ant/core/trunk/docs/manual/CoreTasks/mail.html
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/email/EmailTask.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/email/Mailer.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/email/MimeMailer.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/email/PlainMailer.java

Modified: ant/core/trunk/WHATSNEW
URL: http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=704528&r1=704527&r2=704528&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Tue Oct 14 06:12:23 2008
@@ -425,6 +425,10 @@
    used by MailLogger.
    Bugzilla Report 27211.
 
+ * a new attribute of <mail> allows the task to succeed if it can
+   reach at least one given recipient.
+   Bugzilla Report 36446.
+
 Changes from Ant 1.7.0 TO Ant 1.7.1
 =============================================
 

Modified: ant/core/trunk/docs/manual/CoreTasks/mail.html
URL: http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/CoreTasks/mail.html?rev=704528&r1=704527&r2=704528&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/CoreTasks/mail.html (original)
+++ ant/core/trunk/docs/manual/CoreTasks/mail.html Tue Oct 14 06:12:23 2008
@@ -172,6 +172,13 @@
     <td valign="top">Email subject line.</td>
     <td align="center" valign="top">No</td>
   </tr>
+  <tr>
+    <td valign="top">ignoreInvalidRecipients</td>
+    <td valign="top">Boolean.  Whether the task should try to send
+      the message to as many recipients as possible and should only
+      fail if neither is reachable.  <em>Since Ant 1.8.0</em>.</td>
+    <td align="center" valign="top">No, default is false</td>
+  </tr>
 </table>
 
 <h3>Note regarding the attributes containing email addresses</h3>

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/email/EmailTask.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/email/EmailTask.java?rev=704528&r1=704527&r2=704528&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/email/EmailTask.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/email/EmailTask.java Tue Oct 14
06:12:23 2008
@@ -103,6 +103,9 @@
     /** indicate if the user wishes SSL-TLS */
     private boolean ssl = false;
 
+    /** ignore invalid recipients? */
+    private boolean ignoreInvalidRecipients = false;
+
     /**
      * Set the user for SMTP auth; this requires JavaMail.
      * @param user the String username.
@@ -403,6 +406,19 @@
     }
 
     /**
+     * Whether invalid recipients should be ignored (but a warning
+     * will be logged) instead of making the task fail.
+     *
+     * <p>Even with this property set to true the task will still fail
+     * if the mail couldn't be sent to any recipient at all.</p>
+     *
+     * @since Ant 1.8.0
+     */
+    public void setIgnoreInvalidRecipients(boolean b) {
+        ignoreInvalidRecipients = b;
+    }
+
+    /**
      * Send an email.
      */
     public void execute() {
@@ -532,6 +548,7 @@
             mailer.setTask(this);
             mailer.setIncludeFileNames(includeFileNames);
             mailer.setHeaders(headers);
+            mailer.setIgnoreInvalidRecipients(ignoreInvalidRecipients);
 
             // send the email
             mailer.send();

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/email/Mailer.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/email/Mailer.java?rev=704528&r1=704527&r2=704528&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/email/Mailer.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/email/Mailer.java Tue Oct 14 06:12:23
2008
@@ -48,6 +48,7 @@
     protected boolean includeFileNames = false;
     protected Vector headers = null;
     // CheckStyle:VisibilityModifier ON
+    private boolean ignoreInvalidRecipients = false;
 
     /**
      * Set the mail server.
@@ -206,6 +207,28 @@
          throws BuildException;
 
     /**
+     * Whether invalid recipients should be ignored (but a warning
+     * will be logged) instead of making the task fail.
+     *
+     * <p>Even with this property set to true the task will still fail
+     * if the mail couldn't be sent to any recipient at all.</p>
+     *
+     * @since Ant 1.8.0
+     */
+    public void setIgnoreInvalidRecipients(boolean b) {
+        ignoreInvalidRecipients = b;
+    }
+
+    /**
+     * Whether invalid recipients should be ignored.
+     *
+     * @since Ant 1.8.0
+     */
+    protected boolean shouldIgnoreInvalidRecipients() {
+        return ignoreInvalidRecipients;
+    }
+
+    /**
      * Return the current Date in a format suitable for a SMTP date
      * header.
      *

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/email/MimeMailer.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/email/MimeMailer.java?rev=704528&r1=704527&r2=704528&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/email/MimeMailer.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/email/MimeMailer.java Tue Oct 14
06:12:23 2008
@@ -38,19 +38,22 @@
 import javax.activation.DataHandler;
 import javax.activation.FileDataSource;
 
-import javax.mail.Message;
-import javax.mail.Session;
-import javax.mail.Transport;
 import javax.mail.Authenticator;
+import javax.mail.Address;
+import javax.mail.Message;
 import javax.mail.MessagingException;
 import javax.mail.PasswordAuthentication;
-import javax.mail.internet.MimeMessage;
+import javax.mail.SendFailedException;
+import javax.mail.Session;
+import javax.mail.Transport;
+import javax.mail.internet.AddressException;
+import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeBodyPart;
+import javax.mail.internet.MimeMessage;
 import javax.mail.internet.MimeMultipart;
-import javax.mail.internet.InternetAddress;
-import javax.mail.internet.AddressException;
 
 import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
 
 /**
  * Uses the JavaMail classes to send Mime format email.
@@ -60,6 +63,9 @@
 public class MimeMailer extends Mailer {
     private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
 
+    private static final String GENERIC_ERROR =
+        "Problem while sending mime mail:";
+
     /** Default character set */
     private static final String DEFAULT_CHARSET
         = System.getProperty("file.encoding");
@@ -237,11 +243,36 @@
                 attachments.addBodyPart(body);
             }
             msg.setContent(attachments);
-            Transport.send(msg);
+            try {
+                Transport.send(msg);
+            } catch (SendFailedException sfe) {
+                if (!shouldIgnoreInvalidRecipients()) {
+                    throw new BuildException(GENERIC_ERROR, sfe);
+                } else if (sfe.getValidSentAddresses() == null
+                           || sfe.getValidSentAddresses().length == 0) {
+                    throw new BuildException("Couldn't reach any recipient",
+                                             sfe);
+                } else {
+                    Address[] invalid = sfe.getInvalidAddresses();
+                    if (invalid == null) {
+                        invalid = new Address[0];
+                    }
+                    for (int i = 0; i < invalid.length; i++) {
+                        didntReach(invalid[i], "invalid", sfe);
+                    }
+                    Address[] validUnsent = sfe.getValidUnsentAddresses();
+                    if (validUnsent == null) {
+                        validUnsent = new Address[0];
+                    }
+                    for (int i = 0; i < validUnsent.length; i++) {
+                        didntReach(validUnsent[i], "valid", sfe);
+                    }
+                }
+            }
         } catch (MessagingException e) {
-            throw new BuildException("Problem while sending mime mail:", e);
+            throw new BuildException(GENERIC_ERROR, e);
         } catch (IOException e) {
-            throw new BuildException("Problem while sending mime mail:", e);
+            throw new BuildException(GENERIC_ERROR, e);
         }
     }
 
@@ -274,6 +305,17 @@
         return token.nextToken();
     }
 
+    private void didntReach(Address addr, String category,
+                            MessagingException ex) {
+        String msg = "Failed to send mail to " + category + " address "
+            + addr + " because of " + ex.getMessage();
+        if (task != null) {
+            task.log(msg, Project.MSG_WARN);
+        } else {
+            System.err.println(msg);
+        }
+    }
+
     static class SimpleAuthenticator extends Authenticator {
         private String user = null;
         private String password = null;

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/email/PlainMailer.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/email/PlainMailer.java?rev=704528&r1=704527&r2=704528&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/email/PlainMailer.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/email/PlainMailer.java Tue Oct 14
06:12:23 2008
@@ -24,6 +24,7 @@
 import java.io.PrintStream;
 import java.util.Enumeration;
 import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
 import org.apache.tools.mail.MailMessage;
 
 /**
@@ -44,6 +45,7 @@
             mailMessage.from(from.toString());
 
             Enumeration e;
+            boolean atLeastOneRcptReached = false;
 
             e = replyToList.elements();
             while (e.hasMoreElements()) {
@@ -51,15 +53,36 @@
             }
             e = toList.elements();
             while (e.hasMoreElements()) {
-                mailMessage.to(e.nextElement().toString());
+                String to = e.nextElement().toString();
+                try {
+                    mailMessage.to(to);
+                    atLeastOneRcptReached = true;
+                } catch (IOException ex) {
+                    badRecipient(to, ex);
+                }
             }
             e = ccList.elements();
             while (e.hasMoreElements()) {
-                mailMessage.cc(e.nextElement().toString());
+                String to = e.nextElement().toString();
+                try {
+                    mailMessage.cc(to);
+                    atLeastOneRcptReached = true;
+                } catch (IOException ex) {
+                    badRecipient(to, ex);
+                }
             }
             e = bccList.elements();
             while (e.hasMoreElements()) {
-                mailMessage.bcc(e.nextElement().toString());
+                String to = e.nextElement().toString();
+                try {
+                    mailMessage.bcc(to);
+                    atLeastOneRcptReached = true;
+                } catch (IOException ex) {
+                    badRecipient(to, ex);
+                }
+            }
+            if (!atLeastOneRcptReached) {
+                throw new BuildException("Couldn't reach any recipient");
             }
             if (subject != null) {
                 mailMessage.setSubject(subject);
@@ -135,5 +158,19 @@
             finstr.close();
         }
     }
+
+    private void badRecipient(String rcpt, IOException reason) {
+        String msg = "Failed to send mail to " + rcpt;
+        if (shouldIgnoreInvalidRecipients()) {
+            msg += " because of :" + reason.getMessage();
+            if (task != null) {
+                task.log(msg, Project.MSG_WARN);
+            } else {
+                System.err.println(msg);
+            }
+        } else {
+            throw new BuildException(msg, reason);
+        }
+    }
 }
 



Mime
View raw message