W3C home > Mailing lists > Public > www-validator-cvs@w3.org > October 2009

2006/unicorn/src/org/w3c/unicorn/util Mail.java,1.3,1.4

From: Thomas Gambet via cvs-syncmail <cvsmail@w3.org>
Date: Mon, 12 Oct 2009 13:07:04 +0000
To: www-validator-cvs@w3.org
Message-Id: <E1MxKcC-0007OR-Qa@lionel-hutz.w3.org>
Update of /sources/public/2006/unicorn/src/org/w3c/unicorn/util
In directory hutz:/tmp/cvs-serv28398/src/org/w3c/unicorn/util

Modified Files:
	Mail.java 
Log Message:
added logger 
+email address validity check

Index: Mail.java
===================================================================
RCS file: /sources/public/2006/unicorn/src/org/w3c/unicorn/util/Mail.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- Mail.java	9 Oct 2009 11:13:10 -0000	1.3
+++ Mail.java	12 Oct 2009 13:07:02 -0000	1.4
@@ -13,22 +13,26 @@
 import java.util.Map;
 import java.util.Properties;
 
-import javax.mail.Address;
 import javax.mail.Authenticator;
 import javax.mail.MessagingException;
 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 org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.w3c.unicorn.exceptions.UnicornException;
 import org.w3c.unicorn.output.FileOutputFormater;
 import org.w3c.unicorn.output.OutputFormater;
 
 public class Mail {
 
+	private static Log logger = LogFactory.getLog(Mail.class);
+	
 	public void sendMail(String[] recipients, String subject, List<OutputFormater> outputFormaters, Map<String, Object> contextObjects) throws UnicornException {
 		
 		try {
@@ -46,13 +50,25 @@
 		    InternetAddress addressFrom = new InternetAddress(mailProps.getProperty("unicorn.mail.from"), "Unicorn");
 			msg.setFrom(addressFrom);
 
-			Address[] recipientAdresses = new Address[recipients.length];
-			int i = 0;
+			List<InternetAddress> validAddresses = new ArrayList<InternetAddress>();
+			
 			for (String recipient : recipients) {
-				recipientAdresses[i] = new InternetAddress(recipient);
+				try {
+					InternetAddress address = new InternetAddress(recipient);
+					validAddresses.add(address);
+				} catch(AddressException e) {
+					logger.warn("Invalid address: \"" + recipient + "\". Skipping.");
+				}
+			}
+			
+			InternetAddress[] recipientsAddresses = new InternetAddress[validAddresses.size()];
+			int i = 0;
+			for (InternetAddress add : validAddresses) {
+				recipientsAddresses[i] = add;
 				i++;
 			}
-			msg.setRecipients(javax.mail.Message.RecipientType.TO, recipientAdresses);
+			
+			msg.setRecipients(javax.mail.Message.RecipientType.TO, recipientsAddresses);
 			msg.setSubject(subject);
 			
 			if (outputFormaters.size() > 1) {
Received on Monday, 12 October 2009 13:07:06 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 7 January 2015 15:17:39 UTC