protected Message[] getMessagesByIds(Folder folder, List<String> messageIds) throws MessagingException { List<MessageIDTerm> idTerms = messageIds.stream() .map(MessageIDTerm::new) .collect(Collectors.toList()); OrTerm searchTerm = new OrTerm(idTerms.toArray(new MessageIDTerm[idTerms.size()])); return folder.search(searchTerm); }