使用Exchange Web服务Java API下载附件? [英] Download attachments using Exchange Web Services Java API?
问题描述
我正在编写一个Java应用程序来使用Exchange Web服务下载电子邮件。我正在使用Microsoft的ewsjava API来执行此操作。
I am writing a Java application to download emails using Exchange Web Services. I am using Microsoft's ewsjava API for doing this.
我可以获取电子邮件标头。但是,我无法使用此API下载电子邮件附件。以下是代码段。
I am able to fetch email headers. But, I am not able to download email attachments using this API. Below is the code snippet.
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;
}
}
}
item.getHasAttachments()
返回 true
,但 attachmentsCol.getCount()
是 0
。
推荐答案
您需要加载属性附件
,然后才能在代码中使用它们。您将其设置为传递给FindItems方法的 ItemView
对象。
You need to load property Attachments
before you can use them in your code. You set it for ItemView
object that you pass to FindItems method.
或者您可以先找到项目,然后再调用 service.LoadPropertiesForItems
并传递 findIesults
和 PropertySet
添加的对象 EmailMessageSchema.Attachments
Or you can first find items and then call service.LoadPropertiesForItems
and pass findIesults
and PropertySet
object with added EmailMessageSchema.Attachments
这篇关于使用Exchange Web服务Java API下载附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!