4 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊
對(duì)于 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貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊
每隔一段時(shí)間,我都會(huì)對(duì)著 Microsoft 文檔大罵 Java 實(shí)現(xiàn)(大多數(shù)時(shí)候是不完整的),而且我花在升級(jí)客戶端上的時(shí)間比我想花的時(shí)間還長(zhǎng)。所以我在這里復(fù)制我自己的答案??赡苡腥擞X得這很有用。
從 microsoft-graph 版本 5.34.0 開始,不再需要一些東西,比如解碼,您將能夠簡(jiǎn)單地轉(zhuǎn)換您的對(duì)象。
獲取 File/ItemAttachments 的復(fù)雜方法:
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貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個(gè)贊
我知道這是一個(gè)老問題,但我偶然發(fā)現(xiàn)了這個(gè)問題并認(rèn)為我可以分享我們的解決方案。注意 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貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊
有幾種派生類型的附件。
文件附件
項(xiàng)目附件
參考附件
如果任何附件不是 FileAttachment,那么您將得到這種強(qiáng)制轉(zhuǎn)換異常。在嘗試將其轉(zhuǎn)換為 FileAttachment 之前,您需要測(cè)試附件的類型。
添加回答
舉報(bào)