我正在尝试使用Microsoft Graph SDK让contentBytes下载办公邮件附件。
我尝试了下面的方法,但它给出了ClassCastException。我不知道如何通过Microsoft Graph SDK(Java)直接获取FileAttachment对象的列表
IAttachmentCollectionPage aAttachementPage = clientAuthGraphServiceClient.users("#userIDGiven#").mailFolders("Inbox").messages(message.id).attachments().buildRequest().get();
for (Attachment aAttachment:aAttachementPage.getCurrentPage()) {
String serviceDirectory="D:\\DOWNLOADS\\";
File fileDown= new File(serviceDirectory+aAttachment.name);
Files.write(fileDown.toPath(), ((FileAttachment)aAttachment).contentBytes);
}我正在使用以下jar:
<dependency>
<groupId>com.microsoft.graph</groupId>
<artifactId>microsoft-graph</artifactId>
<version>1.4.0</version>
</dependency>发布于 2019-08-30 11:08:34
有几种派生的依恋类型。
https://docs.microsoft.com/en-us/graph/api/resources/attachment?view=graph-rest-1.0
如果任何附件不是FileAttachment,那么您将得到这种类型的强制转换异常。在尝试将附件转换为FileAttachment之前,您需要测试附件的类型。
发布于 2022-03-01 09:04:14
我知道这是一个古老的问题,但我偶然发现了这个问题,并认为我可以分享我们的解决方案。注意NPE或IndexOutOfBoundsExceptions。
MessageCollectionPage messageCollectionPage = graphClient.me().messages().buildRequest().get();
List<String> messageIdsWithAttachments =
messageCollectionPage.getCurrentPage().stream()
.filter(message -> message.hasAttachments)
.map(message -> message.id)
.toList();
AttachmentCollectionPage attachmentCollectionPage =
graphClient
.me()
.messages(messageIdsWithAttachments.get(0))
.attachments()
.buildRequest()
.get();
byte[] bytes = ((FileAttachment) attachmentCollectionPage.getCurrentPage().get(0)).contentBytes;https://stackoverflow.com/questions/57551352
复制相似问题