before checking item.getHasAttachments (), you must do item.load (). Otherwise, it is likely that your code will not load the attachment, and attachmentsCol.getCount () will be 0. Working code with Exchange Server 2010:
ItemView view = new ItemView(Integer.MAX_VALUE); view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Descending); FindItemsResults < Item > results = service.findItems(WellKnownFolderName.Inbox, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, true), view); Iterator<Item> itr = results.iterator(); while(itr.hasNext()) { Item item = itr.next(); item.load(); ItemId itemId = item.getId(); EmailMessage email = EmailMessage.bind(service, itemId); if (item.getHasAttachments()) { System.err.println(item.getAttachments()); AttachmentCollection attachmentsCol = item.getAttachments(); for (int i = 0; i < attachmentsCol.getCount(); i++) { FileAttachment attachment=(FileAttachment)attachmentsCol.getPropertyAtIndex(i); attachment.load("C:\\TEMP\\" +attachment.getName()); } } }
source share