如何在 java 中使用 Oauth Graph Service Client api 检索 office 365 邮件文件(如图像、文本文件等)附件?

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 邮件文件(如图像、文本文件等)附件?)

本文介绍了如何在 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 邮件文件(如图像、文本文件等)附件?

基础教程推荐