1 17 18 package org.apache.james.transport.matchers; 19 20 import org.apache.mailet.GenericMatcher; 21 import org.apache.mailet.Mail; 22 23 import javax.mail.MessagingException ; 24 import javax.mail.Multipart ; 25 import javax.mail.Part ; 26 import javax.mail.internet.MimeMessage ; 27 import java.util.Collection ; 28 29 34 public class HasAttachment extends GenericMatcher { 35 36 40 public Collection match(Mail mail) throws MessagingException { 41 42 Exception anException = null; 43 44 try { 45 MimeMessage message = mail.getMessage(); 46 Object content; 47 48 52 if (message.getContentType() == null) { 53 return null; 54 } 55 56 content = message.getContent(); 57 if (content instanceof Multipart ) { 58 Multipart multipart = (Multipart ) content; 59 for (int i = 0; i < multipart.getCount(); i++) { 60 try { 61 Part part = multipart.getBodyPart(i); 62 String fileName = part.getFileName(); 63 if (fileName != null) { 64 return mail.getRecipients(); } 66 } catch (MessagingException e) { 67 anException = e; 68 } } 70 } else { 71 String fileName = message.getFileName(); 72 if (fileName != null) { 73 return mail.getRecipients(); } 75 } 76 } catch (Exception e) { 77 anException = e; 78 } 79 80 if (anException != null) { 82 throw new MessagingException ("Malformed message", anException); 83 } 84 85 return null; } 87 } 88 | Popular Tags |