How to get Attachment body content using MS Graph API?

Posey, Jay 20 Reputation points
2023-09-07T22:27:33.6033333+00:00

I am using com.microsoft.graph.* to interface with email messages in Java code. I am having a problem getting the body content from attachments. After the code below runs in my debugger I can see the item (Message) part of the attachment (ItemAttachment) and I can see the content field I want in the body (ItemBody). But I don't see a way to get that attachment body content using the Graph API (microsoft-graph-5.66.0).

LinkedList<Option> requestOptions = new LinkedList<Option>();
requestOptions.add(new QueryOption("$expand", "microsoft.graph.itemattachment/item"));
AttachmentCollectionPage attachments = graphClient.users(mailbox.getName())
	.messages(msg.getMessageId()).attachments().buildRequest(requestOptions).get();
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,454 questions
0 comments No comments
{count} votes

Accepted answer
  1. CarlZhao-MSFT 43,011 Reputation points
    2023-09-08T09:08:16.3166667+00:00

    Hi @Posey, Jay

    Please refer to the code snippet below to get the attachment body content.

    final InputStream result = new FileAttachmentStreamRequestBuilder(graphClient.users("user id").messages("message id").attachments("attachment id").buildRequest().getRequestUrl().toString() + "/$value", graphClient, null).buildRequest().get();
    

    User's image

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.


1 additional answer

Sort by: Most helpful
  1. Manu Philip 18,696 Reputation points MVP
    2023-09-08T03:35:43.2333333+00:00

    Following REST API can show read the email in inbox folder with message id <AAMkAGQzZTJkMGJhLWUwZTctNDQzYi05MzNkLTNlOTUwYjM0NTljYgBGAAAAAADvqaHYVyelQI7v5MqY8Na3BwDyuBU6hMnzRqFR49wpk5oeAAAAAAEMAADyuBU6hMnzRqFR49wpk5oeAAepBfn8AAA=>and read the attachment as "contentBytes":

    https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/Messages/AAMkAGQzZTJkMGJhLWUwZTctNDQzYi05MzNkLTNlOTUwYjM0NTljYgBGAAAAAADvqaHYVyelQI7v5MqY8Na3BwDyuBU6hMnzRqFR49wpk5oeAAAAAAEMAADyuBU6hMnzRqFR49wpk5oeAAepBfn8AAA=/attachments/?value

    Hope, this will help to frame your query


    --please don't forget to upvote and Accept as answer if the reply is helpful--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.