fetching attached itemAttachements of office365 emails using Graph API

Supriya A 1 Reputation point
2022-06-16T14:11:19.16+00:00

Hi,

I need to download attachments from selected Office365 email recursively.

I tried below request from one of the solution provided online,

RequestUri = {https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/messages/{msgID}/attachments/{attachedmsgID}=/?$expand = microsoft.graph.itemattachment/item}

but getting below response.

StatusCode: 404, ReasonPhrase: 'Not Found'

can anyone please let me know how do I fetch itemAttachments and its attachments?

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
23,912 questions
No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Mohammed Mehtab Siddique (MINDTREE LIMITED) 731 Reputation points Microsoft Employee
    2022-06-16T19:08:00.323+00:00

    Hi @Supriya A ,

    For getting the itemAttachements of mail please follow the below instructions:

    Expand and get the properties of the item attached to a message:

    GET https://graph.microsoft.com/v1.0/me/messages/AAMkADA1M-zAAA=/attachments/AAMkADA1M-CJKtzmnlcqVgqI=/?$expand=microsoft.graph.itemattachment/item  
    

    Expand and get the properties of an item attached to a message, including any attachment to the item:

    GET https://graph.microsoft.com/v1.0/me/messages/AAMkADA1M-zAAA=/attachments/AAMkADA1M-CJKtzmnlcqVgqI=/?$expand=microsoft.graph.itemattachment/item  
    

    For fetching these details you need to have the following permission:

    212224-image.png

    You can also decode your Token in the https://jwt.ms/ and check in scope whether these permissions are already available.

    ----------

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


  2. AtulThorat-MSFT 386 Reputation points
    2022-06-16T19:19:15.56+00:00

    Hi @Supriya A ,

    Hope you are doing well.

    As per documentation,attachment-get and query-parameters we can get attachment of mail message and can use query parameters to customize responses.

    $expand query parameter can be used as shown below. I have tried this in graph explorer and I could able to get correct response as shown below.

    GET https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/messages/{messageID}/attachments/{attachmentID}$expand=microsoft.graph.itemattachment/item

    212127-attachment.png

    212147-attachment-response.png

    So we are unable to reproduce this issue.

    You are getting StatusCode: 404, ReasonPhrase: 'Not Found' so Please try to use graph api endpoint attachment-get and check weather this attachment file is still available for particular mail message or not.

    Hope this helps.

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


  3. Supriya A 1 Reputation point
    2022-06-17T06:03:05.457+00:00

    Hi, I was not able to comment so writing an answer,
    @AtulThorat-MSFT , I tried with below endpoint,

    RequestUri = {https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/messages/{msg-id}/attachments/{attachment-id}/?$Value}

    and getting below response,

    {
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('supriyatest2021%40outlook.com')/mailFolders('Inbox')/messages('AQMkADAwATM3ZmYAZS00NjJhLWFiYgA2LTAwAi0wMAoARgAAA4Amv2sM8nJEo5QlvuMbUbMHAOyxeWKJD4JHrqegw15KyEAAAAIBDAAAAOyxeWKJD4JHrqegw15KyEAAAACEBqjhAAAA')/attachments/$entity",
    "@odata.type": "#microsoft.graph.itemAttachment",
    "id": "AQMkADAwATM3ZmYAZS00NjJhLWFiYgA2LTAwAi0wMAoARgAAA4Amv2sM8nJEo5QlvuMbUbMHAOyxeWKJD4JHrqegw15KyEAAAAIBDAAAAOyxeWKJD4JHrqegw15KyEAAAACEBqjhAAAAARIAEACURMvi8EaiSpv2FuT4p1iS",
    "lastModifiedDateTime": "2022-06-15T05:16:48Z",
    "name": "test 1",
    "contentType": "message/rfc822",
    "size": 8575119,
    "isInline": false
    }

    means the itemAttachment is present, then not sure why I am not able to get the item itself, when I try below?

    {https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/messages/{msgID}/attachments/{attachedmsgID}=/?$expand = microsoft.graph.itemattachment/item}

    Also this itemAttachment has another fileAttachment, but I do not get ContentID in above response( with $Value)?