Get contentBytes() from attachment of an email

Ojas Pawaskar 0 Reputation points
2024-03-22T10:07:06.34+00:00

Hi, I am currently working on a Springboot Java project which is using MS graph API version 6.4.0 to read and move mails. After migration from version 2.10.0, and after multiple changes to existing code, I'm getting an error when reading the content of an attachment. Previously to get content the code was
String content = jsonArrayForAttachment.get(i).getAsJsonObject().get("contentBytes");
Where jsonArrayForAttachment is JsonArray and data stored is taken from

attachmentPage.getRawObject().get("value").getAsJsonArray();

Now I have changed above code to the following:
AttachmentCollectionResponse attachmentPage = null;
List<Attachment> attachmentValueList= attachmentPage.getValue();

The issue I'm facing is that there is no method: getContentBytes() inside the attachmentValueList.get(0) Attachment object.
I then use the content String like this : attachmentStream = new ByteArrayInputStream(Base64.getUrlDecoder().decode(content));

I need the contentBytes data to then read the contents of an attachment and then write that content to a new temporary location.

Any help would be helpful.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,445 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Deepanshu Sharma 490 Reputation points Microsoft Vendor
    2024-03-31T19:38:10.7366667+00:00

    Hi Ojas,

    you can use :-

    var fileAttachment = attachmentRequest.Result as Microsoft.Graph.FileAttachment;