4 回答
TA贡献1829条经验 获得超9个赞
对于 GraphApi 版本 2.5.0
AttachmentCollectionPage attachmentCollectionPage = graphClient .me() .messages(messageId) .attachments() .buildRequest() .get(); JsonElement att = attachmentCollectionPage .getCurrentPage() .get(0) .getRawObject() .get("contentBytes"); byte[] decodedBytes = Base64.getDecoder().decode(att.getAsString());
TA贡献1966条经验 获得超4个赞
每隔一段时间,我都会对着 Microsoft 文档大骂 Java 实现(大多数时候是不完整的),而且我花在升级客户端上的时间比我想花的时间还长。所以我在这里复制我自己的答案。可能有人觉得这很有用。
从 microsoft-graph 版本 5.34.0 开始,不再需要一些东西,比如解码,您将能够简单地转换您的对象。
获取 File/ItemAttachments 的复杂方法:
List<Attachment> attachments = new ArrayList<>();
try {
AttachmentCollectionPage mgAttachmentList =
graphClient.users(emailAddress)
.messages(mail.id)
.attachments()
.buildRequest()
.get();
do {
attachments.addAll(mgAttachmentList.getCurrentPage());
// Check for next page
if (mgAttachmentList.getNextPage() != null) {
mgAttachmentList = mgAttachmentList.getNextPage().buildRequest().get();
} else {
mgAttachmentList = null;
}
} while (mgAttachmentList != null);
attachments.stream()
.filter(Objects::nonNull)
.forEach(attachment -> {
try {
// attachment equals a FileAttachment
if ("#microsoft.graph.fileAttachment".equalsIgnoreCase(attachment.oDataType)) {
byte[] attachmentContentBytes = ((FileAttachment) attachment).contentBytes;
if (attachmentContentBytes != null) {
// your own logic here
}
} else if ("#microsoft.graph.itemAttachment".equalsIgnoreCase(attachment.oDataType)) {
/*
* We need to convert each Attachment.
* If it's an ItemAttachment, we need to do another call to retrieve the item-object.
*
* The standard call with expand option gives us an Attachment (again)
*/
if (StringUtils.isNotBlank(attachment.contentType)) {
Attachment attachment = graphClient.users(emailAddress)
.messages(mail.id)
.attachments(attachment.id)
.buildRequest()
.expand("microsoft.graph.itemattachment/item")
.get();
if (attachment != null) {
// cast the object to whatever you need, in this example I retrieve the webLink
String linkToMessage = ((Message) ((ItemAttachment) attachment).item).webLink;
// your own logic here
}
} else {
log.info("Unknown attachment contentType for an ItemAttachment - Could not read attachment.");
}
} else {
log.error("Unable to read an attachment for mail.");
}
} catch (Exception e) {
log.error("Could not read attachment: '" + attachment.name + "' for message: '" + message.subject + ". " + e.getMessage(), e);
}
});
} catch (Exception e) {
log.error("Something went wrong while receiving attachments for message: '" + message.subject + "'. " + e.getMessage(), e);
}
TA贡献1712条经验 获得超3个赞
我知道这是一个老问题,但我偶然发现了这个问题并认为我可以分享我们的解决方案。注意 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;
TA贡献1829条经验 获得超13个赞
有几种派生类型的附件。
文件附件
项目附件
参考附件
如果任何附件不是 FileAttachment,那么您将得到这种强制转换异常。在尝试将其转换为 FileAttachment 之前,您需要测试附件的类型。
添加回答
举报