How to read email attachments using MS Graph API

Mayank Gupta 5 Reputation points
2024-10-15T11:47:08.7066667+00:00

How to read email attachments using MS Graph API and how to mark mail as read once mail is read by graph API

Microsoft Security Microsoft Graph
{count} vote

3 answers

Sort by: Most helpful
  1. Adharsh Santhanam 6,015 Reputation points Volunteer Moderator
    2024-10-15T12:03:57.3933333+00:00

    Hello Mayank Gupta, first retrieve the email message to get its ID. You can use the "list messages" API for this. Once you have the message ID, you can retrieve the attachments using the "get attachments" API post which you can append the /$value to the request URL to get the raw content of a file attachment. So, the workflow would be something like the below.

    GET https://graph.microsoft.com/v1.0/me/messages

    GET https://graph.microsoft.com/v1.0/me/messages/{message-id}/attachments

    GET https://graph.microsoft.com/v1.0/me/messages/{message-id}/attachments/{attachment-id}/$value

    Similarly, to mark an email as read, you need to update the isRead property of the message. It would be something like the below.

    PATCH https://graph.microsoft.com/v1.0/me/messages/{message-id}

    Content-Type: application/json

    {

    "isRead": true

    }

    You may want to read this documentation page - https://learn.microsoft.com/en-us/graph/api/attachment-get?view=graph-rest-1.0&tabs=http

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.


  2. Rohit Raghuwanshi - MSFT 625 Reputation points Microsoft External Staff
    2024-10-15T12:10:55.5866667+00:00

    Hi Mayank Gupta,

    Thank you for reaching out, Microsoft!

    You can use below endpoint to read email attachments and mark an email as read using the Microsoft Graph API.

    Part 1: Reading Email Attachments

    Set Up Permissions:

    • Ensure your application has Mail.Read or Mail.ReadWrite permissions.
    1. Get the Message ID:
      • Use the list messages API to retrieve emails from the inbox. This will give you the message IDs.
         GET https://graph.microsoft.com/v1.0/me/messages
      
    2. Retrieve Attachments:
      • Use the list attachments API to get the attachments for a specific message.
         GET https://graph.microsoft.com/v1.0/me/messages/{message-id}/attachments
      
      1. Download Attachment:
        • To download the attachment, use the get attachment API.
              GET https://graph.microsoft.com/v1.0/me/messages/{message-id}/attachments/{attachment-id}/$value
        

    Part 2: Marking an Email as Read

    1. Set Up Permissions:
      • Ensure your application has Mail.ReadWrite permissions.
    2. Mark as Read:
      • Use the update message API to mark the email as read.
         PATCH https://graph.microsoft.com/v1.0/me/messages/{message-id}
      
      Please check below screenshot for details:
      1. Before Patch request:
        User's image
      2. After Patch request to update isRead property:
      User's image Hope this helps. References:
      Attachments Graph API
      Update-Messages Graph API If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    2 deleted comments

    Comments have been turned off. Learn more

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.