使用Exchange Web服务Java API下载附件?

Nar*_*hPS 7 java exchange-server exchangewebservices ewsjavaapi

我正在编写一个Java应用程序来使用Exchange Web服务下载电子邮件.我正在使用Microsoft的ewsjava API来执行此操作.

我可以获取电子邮件标题.但是,我无法使用此API下载电子邮件附件.以下是代码段.

FolderId folderId = new FolderId(WellKnownFolderName.Inbox, "mailbox@example.com");
findResults = service.findItems(folderId, view);
for(Item item : findResults.getItems()) {
   if (item.getHasAttachments()) {
      AttachmentCollection attachmentsCol = item.getAttachments();
      System.out.println(attachmentsCol.getCount()); // This is printing zero all the time. My message has one attachment.
      for (int i = 0; i < attachmentsCol.getCount(); i++) {
         FileAttachment attachment = (FileAttachment)attachmentsCol.getPropertyAtIndex(i);
         String name = attachment.getFileName();
         int size = attachment.getContent().length;
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

item.getHasAttachments()是返回true,但是attachmentsCol.getCount()0.

gra*_*lec 7

您需要先加载属性,Attachments然后才能在代码中使用它们.您为ItemView传递给FindItems方法的对象设置它.

或者你可以先找到项目,然后调用service.LoadPropertiesForItems并传递findIesultsPropertySet添加对象EmailMessageSchema.Attachments


Yur*_*ury 5

FolderId folderId = new FolderId(WellKnownFolderName.Inbox, "mailbox@example.com"); 
findResults = service.findItems(folderId, view); 
service.loadPropertiesForItems(findResults, new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.Attachments));

for(Item item : findResults.getItems()) { 
   if (item.getHasAttachments()) { 
      AttachmentCollection attachmentsCol = item.getAttachments(); 
      System.out.println(attachmentsCol.getCount());
      for (int i = 0; i < attachmentsCol.getCount(); i++) { 
         FileAttachment attachment = (FileAttachment)attachmentsCol.getPropertyAtIndex(i); 
         attachment.load(attachment.getName());
      } 
   } 
} 
Run Code Online (Sandbox Code Playgroud)