Graph Email Attachment does not have Content or ContentBytes attributes

Ashley Fonseca 21 Reputation points
2022-06-22T07:52:10.827+00:00

I am using latest version of Graph and related nuget pakages. I have seen examples on how to download attachment but I cannot find Content or ContentByte attribute Attachment class. How can I get attachment. Below is my code

var attachment = await graphClient.Users[users[0].Id].Messages[messages[0].Id].Attachments[aId.Id]
.Request()
.GetAsync();

Please see attach screenshot.

213620-graphattachment.png

Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

Accepted answer
  1. Srinivasa Rao Darna 6,761 Reputation points Microsoft External Staff
    2022-06-22T09:25:48.237+00:00

    Hi @Anonymous ,

    To get mail attachment you can change your code as follows.

    var stream = await graphClient.Users["{userId}"].Messages["{messageId}"].Attachments["{attachmentId}"].Content  
    	.Request()  
    	.GetAsync();  
    

    Hope this helps.

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


3 additional answers

Sort by: Most helpful
  1. Paul Klinger 15 Reputation points
    2023-10-31T10:43:33.3733333+00:00

    Hi, I recently had the same problem.

    Here is a solution that worked for me and is supported by the v5 of the MS Graph SDK.

    //Get Attachment
    var stream = await graphClient.Users["{userID}"].Messages["{msgId}"].Attachments["{attachmentId}"]
    .GetAsync();
    
    //cast stream to FileAttachment 
    var fileAttachment = stream as FileAttachment; 
    
    //Now you could do something like save the file
    File.WriteAllBytes(filePath, fileAttachment.ContentBytes);
    
    3 people found this answer helpful.
    0 comments No comments

  2. Elias Glor 6 Reputation points
    2022-11-25T13:14:17.79+00:00

    For anyone else having this problem:

    Attachment is a generic type and there are different types of attachments --> https://learn.microsoft.com/en-us/graph/api/resources/attachment?view=graph-rest-1.0
    The Graph Nuget package returns an Attachment object, while the concrete type could be an actual FileAttachment. So to access ContentBytes you need to cast it to its concrete type.

    No need for these workarounds.

    1 person found this answer helpful.

  3. Ashley Fonseca 21 Reputation points
    2022-06-24T07:56:55.807+00:00

    Thanks for your help mate, this workaround works perfectly. I really appriciate your help. You save me lot of trobule and time to write powershell script as a workaround.

    0 comments No comments

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.