How to retrieve office 365 mail file (like image,text file etc) attachment using Oauth Graph Service Client api in java?

五迷三道 提交于 2021-02-08 10:12:33

问题


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()));
}

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?


回答1:


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
            }
        }
    }
}



回答2:


Your method of reading contentBytes from the rawObject JSON will work. You just need to use a Base64 decoder instead of String.getBytes() because contentBytes is base64-encoded. This was a bit faster on my network than requesting the attachment again using FileAttachmentRequestBuilder.

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(
                Base64.getDecoder().decode(attachment.getRawObject()
                    .get("contentBytes")
                    .getAsString());
}


来源:https://stackoverflow.com/questions/60971537/how-to-retrieve-office-365-mail-file-like-image-text-file-etc-attachment-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!