How to retrieve office 365 mail file (like image,text file etc) attachment using Oauth Graph Service Client api in java?(如何在 java 中使用 Oauth Graph Service Client api 检索 office 365 邮件文件(如图像、文本文件等)附件?)
问题描述
这是我如何获取附加到邮件的附件对象列表:
Here is how i am getting list of attachment objects attached to a message:
IAttachmentCollectionRequest attachmentsPage = graphClient
.users(emailServer.getEmailAddress())
.mailFolders("Inbox")
.messages(mail.id)
.attachments()
.buildRequest();
List<Attachment> attachmentsData = attachmentsPage.get().getCurrentPage();
List<AttachmentData> attachmentDataToStore = new java.util.ArrayList<AttachmentData>();
for(Attachment attachment : attachmentsData)
{
attachmentData.setInputStream(
new ByteArrayInputStream(
attachment.getRawObject()
.get("contentBytes")
.getAsString()
.getBytes()));
}
现在,我认为内容字节到输入流的转换没有正确进行,最终数据(image.png)正在损坏.有什么建议吗?
Now, I believe the conversion of content bytes to input stream is not happening properly and eventually data(image.png) is getting corrupted. Any suggestions?
推荐答案
使用 FileAttachment
类为您处理所有这些.不幸的是,SDK 没有以最干净"的方式处理这个问题——您必须再次请求附件.
Use the FileAttachment
class to handle all of this for you. Unfortunately the SDK doesn't handle this in the most "clean" manner - you have to request the attachment a second time.
public static void saveAttachments(String accessToken) {
ensureGraphClient(accessToken);
final String mailId = "message-id";
// Get the list of attachments
List<Attachment> attachments = graphClient
.me()
.messages(mailId)
.attachments()
.buildRequest()
// Use select here to avoid getting the content in this first request
.select("id,contentType,size")
.get()
.getCurrentPage();
for(Attachment attachment : attachments) {
// Attachment is a base type - need to re-get the attachment as a fileattachment
if (attachment.oDataType.equals("#microsoft.graph.fileAttachment")) {
// Use the client to generate the request URL
String requestUrl = graphClient
.me()
.messages(mailId)
.attachments(attachment.id)
.buildRequest()
.getRequestUrl()
.toString();
// Build a new file attachment request
FileAttachmentRequestBuilder requestBuilder =
new FileAttachmentRequestBuilder(requestUrl, graphClient, null);
FileAttachment fileAttachment = requestBuilder.buildRequest().get();
// Get the content directly from the FileAttachment
byte[] content = fileAttachment.contentBytes;
try (FileOutputStream stream = new FileOutputStream("C:\Source\test.png")) {
stream.write(content);
} catch (IOException exception) {
// Handle it
}
}
}
}
这篇关于如何在 java 中使用 Oauth Graph Service Client api 检索 office 365 邮件文件(如图像、文本文件等)附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 java 中使用 Oauth Graph Service Client api 检索 office 365 邮件文件(如图像、文本文件等)附件?
基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 降序排序:Java Map 2022-01-01