Graph API Attachment add not working

Akash Sharma 1 Reputation point
2021-08-25T13:54:21.59+00:00

Encountered something like this working on .Net
While trying to add attachments in Microsoft Graph, using Microsoft Graph Client Library v4.3.0
Able to send mail, get mails , fetch attachments etc

The adding attachment object to a post message
In reply or send mail not working
Tried following these steps as well-
1-Creating a reply (as a draft) endpoint /createReply
2-Adding attachment
3-Sending the mail

In all the above cases the attachment addition is failing with error-

Code- "RequestBodyRead", "message": "The property 'ContentBytes' doesnot exist on type 'microsoft.graph.attachment'.

And ContentBytes is the field supossed to carry the actual attachment as base64.

For attchement types i have tried using

  • MessageAttachmentCollectionsPage
  • PostAttachmentCollectionsPage
  • custom attachment object with only name and contentBytes.

Can anyone guide me regarding this issue.

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

4 answers

Sort by: Most helpful
  1. Manuel Mall 16 Reputation points
    2022-01-25T13:31:15.633+00:00

    Hi,

    just encountered the same issue and the following fixed it:

    When building the file attachment object in your code set the odatatype member to "#microsoft.graph.fileAttachment".

    In Java it looks something like:

    FileAttachment attachment = new FileAttachment();  
    attachment.name = "attachment.txt";  
    attachment.contentType = "text/plain";  
    attachment.contentBytes = Base64.getDecoder().decode("SGVsbG8gV29ybGQh");  
    attachment.oDataType = "#microsoft.graph.fileAttachment";  
    

    The Java code examples in the documentation [https://learn.microsoft.com/en-us/graph/api/user-sendmail] are missing that detail.

    3 people found this answer helpful.
    0 comments No comments

  2. Anders Hedström 11 Reputation points
    2022-06-17T19:58:54.837+00:00

    I think it's pretty bad that the documentation still isn't updated - it's still missing the part about -> attachment.oDataType

    another thing is that is wrong is that if you read the javadoc for FileAttachment.contentBytes it says

       /**  
            * The Content Bytes.  
            * The base64-encoded contents of the file.  
            */  
           @SerializedName(value = "contentBytes", alternate = {"ContentBytes"})  
           @Expose  
       	@Nullable  
           public byte[] contentBytes;  
    

    `

    The javadoc says it should be base64 encoded, the examples shows base64 decoded ( attachment.contentBytes = Base64.getDecoder().decode("SGVsbG8gV29ybGQh"); ) - the examples are correct, not the javadoc...

    This is what's the Achilles heel with documentation - it's so important that it's correct and up to date...and for a company like Microsoft it's even more important. People complain that I don't write that much documentation - but that's because I know I wouldn't be able to keep it up to date, and to be honest, the only users of my code is my colleagues...and we aren't that many...but users of Microsofts programming libraries should and does expect the documentation to be correct and up to date... That's my opinion at least...

    2 people found this answer helpful.

  3. Diana Wanjuhi 1,376 Reputation points
    2021-09-15T14:11:44.16+00:00

    Hello @Akash Sharma Thank you for reaching out. This is happening because the resource type is wrong, the attachment in this case is of type "@odata.type": "#microsoft.graph.fileAttachment" see documentation here: fileattachment

    The microsoft.graph.attachment looks like this: attachment

    {  
      "contentType": "string",  
      "id": "string (identifier)",  
      "isInline": true,  
      "lastModifiedDateTime": "String (timestamp)",  
      "name": "string",  
      "size": 1024  
    }  
    

    To fix this issue, ensure the type is correct.

    Let me know whether this helps, and if you have further questions.

    1 person found this answer helpful.

  4. Elyes Hachem 96 Reputation points
    2021-11-06T10:11:19.197+00:00

    Hi,

    I have the same issue.

    Your documentation is not correct at all.
    I have copy pasted your sample and I am getting a 400 HTTP error code.

    I think the problem is coming from a documentation error.
    I have created a GitHub issue here : https://github.com/microsoftgraph/microsoft-graph-docs/issues/14839

    Regards

    0 comments No comments