Search code examples
c#microsoft-graph-apimicrosoft-graph-sdks

Download raw content of email attachment using Microsoft Graph SDK


Is there a method that allows to download the email attachment content bytes using the SDK?

Basically, it should just append /$value the request URL according to https://learn.microsoft.com/en-us/graph/api/attachment-get?view=graph-rest-1.0&tabs=csharp#example-6-get-the-raw-contents-of-a-file-attachment-on-a-message , but I don't see such feature in SDK

The closest I could get is

await graphClient.Users[userId].Messages[messageId].Attachments[attachmentId].GetAsync()

I would expect Attachments[attachmentId].Content.GetAsync() or something

Microsoft.Graph 5.13.0


Solution

  • It's still not supported by SDK v5 to download message's attachment. The workaround can be

    var requestInfo = graphClient.Users["{user_id}"]
                                 .Messages["{message_id}"]
                                 .Attachments["{attachment_id}"]
                                 .ToGetRequestInformation();
    requestInfo.UrlTemplate = requestInfo.UrlTemplate
                               .Insert(requestInfo.UrlTemplate.LastIndexOf('{'), "/$value");
    var attachmentStream = await graphClient.RequestAdapter.SendPrimitiveAsync<Stream>(requestInfo);