如何在 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...

这是我如何获取附加到邮件的附件对象列表:

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:Sourcetest.png")) {
                stream.write(content);
            } catch (IOException exception) {
                // Handle it
            }
        }
    }
}
阅读全文

相关推荐

最新文章