MessageUtils { static public List<BodyPart> extractMessageAttachments(Log logger, Object content) throws MessagingException, IOException { ArrayList<BodyPart> ret = new ArrayList<BodyPart>(); if (content instanceof Multipart) { Multipart part = (Multipart) content; for (int i = 0; i < part.getCount(); i++) { BodyPart bodyPart = part.getBodyPart(i); String fileName = bodyPart.getFileName(); String[] contentId = bodyPart.getHeader("Content-ID"); if (bodyPart.isMimeType("multipart/*")) { ret.addAll(extractMessageAttachments(logger, bodyPart.getContent())); } else { if (contentId != null || fileName != null) { ret.add(bodyPart); } } } } else { logger.error("Unknown content: " + content.getClass().getName()); } return ret; }  static Address[] getRecipients(String... addresses); static Address[] getRecipients(List<String> recipients); static List<BodyPart> extractMessageAttachments(Log logger, Object content); static List<BodyPart> extractInlineImages(Log logger, Object content); static boolean handleParts(Message message, Object content, StringBuffer sbPlain,
            ArrayList<MessageAttachment> attachmentList); static Part handleMultiPart(Log logger, Object content, String attachmentName); static BodyPart fileitemToBodypart(FileItem item); static String decodeText(String s); static String encodeEmail(String s); static String encodeTexts(String s);  }
@Test public void extractMessageAttachments() throws Exception { Message message = TestUtils.createMockMimeMessage(session, 2); List<BodyPart> parts = MessageUtils.extractMessageAttachments(logger, message.getContent()); assertEquals(2, parts.size()); }