How to get Attachment body content using MS Graph API?

Posey, Jay 20 Reputation points

I am using* 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())
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,448 questions
0 comments No comments
{count} votes

Accepted answer
  1. CarlZhao-MSFT 36,336 Reputation points

    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 16,956 Reputation points MVP

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

    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