Update of /var/cvs/applications/email/src/org/mmbase/applications/email
In directory james.mmbase.org:/tmp/cvs-serv14319

Modified Files:
        SendMail.java 
Log Message:
generalized 'onlyTo' functionlaity a bit further


See also: 
http://cvs.mmbase.org/viewcvs/applications/email/src/org/mmbase/applications/email


Index: SendMail.java
===================================================================
RCS file: 
/var/cvs/applications/email/src/org/mmbase/applications/email/SendMail.java,v
retrieving revision 1.51
retrieving revision 1.52
diff -u -b -r1.51 -r1.52
--- SendMail.java       14 Oct 2008 11:04:32 -0000      1.51
+++ SendMail.java       27 Oct 2008 12:20:10 -0000      1.52
@@ -32,7 +32,7 @@
  * @author Daniel Ockeloen
  * @author Johannes Verelst <[EMAIL PROTECTED]>
  * @since  MMBase-1.6
- * @version $Id: SendMail.java,v 1.51 2008/10/14 11:04:32 michiel Exp $
+ * @version $Id: SendMail.java,v 1.52 2008/10/27 12:20:10 michiel Exp $
  */
 public class SendMail extends AbstractSendMail {
     private static final Logger log = 
Logging.getLoggerInstance(SendMail.class);
@@ -44,7 +44,9 @@
     public static long emailSent = 0;
     public static long emailFailed = 0;
 
-    private Pattern onlyToPattern = Pattern.compile(".*");
+    private static final Pattern MATCH_ALL = Pattern.compile(".*");
+
+    private Pattern onlyToPattern = MATCH_ALL;
 
     public SendMail() {
         this(null);
@@ -208,10 +210,27 @@
         log.debug("Finished processing local mails");
     }
 
+    /**
+     * Like InternetAddress#parse but leaves out the addresses not matching 
'onlyTo'.
+     */
+    protected InternetAddress[] parseOnly(String to) throws MessagingException 
{
+        List<InternetAddress> res = new ArrayList<InternetAddress>();
+        InternetAddress[] parsed = InternetAddress.parse(to);
+        for( InternetAddress a : parsed) {
+            if (onlyToPattern.matcher(a.getAddress()).matches()) {
+                res.add(a);
+            } else {
+                log.service("Skipping " + a + " because it does not match " + 
onlyToPattern);
+            }
+
+        }
+        return res.toArray(parsed);
+    }
+
 
     /**
      */
-    public boolean sendMultiPartMail(String from, String to, Map<String, 
String> headers, MimeMultipart mmpart) throws javax.mail.MessagingException {
+    public boolean sendMultiPartMail(String from, String to, Map<String, 
String> headers, MimeMultipart mmpart) throws MessagingException {
         if (log.isServiceEnabled()) {
             log.service("Sending (multipart) mail from " + from + " to " + to);
             if (log.isDebugEnabled()) {
@@ -219,10 +238,11 @@
             }
 
         }
-        if (onlyToPattern.matcher(to).matches())  {
+        InternetAddress[] onlyTo = parseOnly(to);
+        if (onlyTo.length > 0)  {
 
             try {
-                MimeMessage msg = constructMessage(from, to, headers);
+                MimeMessage msg = constructMessage(from, onlyTo, headers);
                 if (mmpart == null) throw new NullPointerException();
                 msg.setContent(mmpart);
 
@@ -369,24 +389,24 @@
     /**
      * Utility method to do the generic job of creating a MimeMessage object 
and setting its recipients and 'from'.
      */
-    protected MimeMessage constructMessage(String from, String to, Map<String, 
String> headers) throws MessagingException {
+    protected final MimeMessage constructMessage(String from, 
InternetAddress[] to, Map<String, String> headers) throws MessagingException {
         // construct a message
         MimeMessage msg = new MimeMessage(session);
         if (from != null && !from.equals("")) {
             msg.addFrom(InternetAddress.parse(from));
         }
 
-        msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
+        msg.addRecipients(Message.RecipientType.TO, to);
 
         String cc = headers.get("CC");
         if (cc != null) {
             log.info("Adding cc " + cc);
-            msg.addRecipients(Message.RecipientType.CC, 
InternetAddress.parse(cc));
+            msg.addRecipients(Message.RecipientType.CC, parseOnly(cc));
         }
         String bcc = headers.get("BCC");
         if (bcc != null) {
             log.info("Adding bcc " + cc);
-            msg.addRecipients(Message.RecipientType.BCC, 
InternetAddress.parse(bcc));
+            msg.addRecipients(Message.RecipientType.BCC, parseOnly(bcc));
         }
 
         String replyTo = headers.get("Reply-To");
@@ -418,23 +438,25 @@
         if (log.isServiceEnabled()) {
             log.service("Sending mail to " + to + " Headers " + headers + " " 
+ session);
         }
-        if (onlyToPattern.matcher(to).matches()) {
             try {
-                MimeMessage msg = constructMessage(from, to, headers);
+
+            InternetAddress[] onlyTo = parseOnly(to);
+            if (onlyTo.length > 0) {
+                MimeMessage msg = constructMessage(from, onlyTo, headers);
                 msg.setText(data, mailEncoding);
                 Transport.send(msg);
                 log.debug("SendMail done.");
                 return true;
+            } else {
+                log.service("not sending mail to " + to + " because it does 
not match " + onlyToPattern);
+                return true;
+            }
             } catch (MessagingException e) {
                 log.error("SendMail failure: " + e.getClass() + " " + 
e.getMessage() + " from: " + from + " to: " + to + " " + (e.getCause() != null 
? e.getCause() : ""));
                 if (log.isDebugEnabled()) {
                     log.debug("because: ", new Exception());
                 }
             }
-        } else {
-            log.service("not sending mail to " + to + " because it does not 
match " + onlyToPattern);
-            return true;
-        }
         return false;
     }
 
@@ -484,14 +506,7 @@
             }
             msg.setHeader("X-mmbase-node", n.getNodeManager().getName() + "/" 
+ n.getNumber());
             try {
-                InternetAddress[] toRecipients = InternetAddress.parse(to);
-                for (InternetAddress toRecipient : toRecipients) {
-                    if 
(onlyToPattern.matcher(toRecipient.getAddress()).matches()) {
-                        msg.addRecipient(Message.RecipientType.TO, 
toRecipient);
-                    } else {
-                        log.service("Not sending to " + toRecipient + " 
because it does not match " + onlyToPattern);
-                    }
-                }
+                msg.addRecipients(Message.RecipientType.TO, parseOnly(to));
             } catch (javax.mail.internet.AddressException ae) {
                 log.warn(ae);
                 errors.append("\nTo: " + to + ": " + ae.getMessage());
@@ -499,14 +514,7 @@
 
             if (cc != null) {
                 try {
-                    InternetAddress[] ccRecipients = InternetAddress.parse(cc);
-                    for (InternetAddress ccRecipient : ccRecipients) {
-                        if 
(onlyToPattern.matcher(ccRecipient.getAddress()).matches()) {
-                            msg.addRecipient(Message.RecipientType.CC, 
ccRecipient);
-                        } else {
-                            log.service("Not cc-sending to " + ccRecipient + " 
because it does not match " + onlyToPattern);
-                        }
-                    }
+                    msg.addRecipients(Message.RecipientType.CC, parseOnly(cc));
                 } catch (javax.mail.internet.AddressException ae) {
                     log.warn(ae);
                     errors.append("\nCc: " + cc  + " " + ae.getMessage());
@@ -515,14 +523,7 @@
 
             if (bcc != null) {
                 try {
-                    InternetAddress[] bccRecipients = 
InternetAddress.parse(bcc);
-                    for (InternetAddress bccRecipient : bccRecipients) {
-                        if 
(onlyToPattern.matcher(bccRecipient.getAddress()).matches()) {
-                            msg.addRecipients(Message.RecipientType.BCC, 
bccRecipients);
-                        } else {
-                            log.service("Not cc-sending to " + bccRecipient + 
" because it does not match " + onlyToPattern);
-                        }
-                    }
+                    msg.addRecipients(Message.RecipientType.BCC, 
parseOnly(bcc));
                 } catch (javax.mail.internet.AddressException ae) {
                     log.warn(ae);
                     errors.append("\nBcc: " + bcc + " " + ae.getMessage());
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to