Search code examples
azuremicrosoft-graph-api

MS Graph API Error - contentBytes not a property of type microsoft.graph.attachment


We are interacting with the API via HTTP. Please don't post answers or questions about using the SDK.

POST https://graph.microsoft.com/v1.0/me/sendMail
Content-type: application/json

{
    "saveToSentItems": "false",
    "message": {
        "toRecipients": [
            {"emailAddress": {"address": "[email protected]"}},
            {"emailAddress": {"address": "[email protected]"}}
        ],
        "attachments": [
            {
                "name": "attachment-1.txt",
                "contentBytes": "QXR0YWNobWVudCBPbmUgLQpUaGlzIGlzIGEgdGVzdCBzZW5kIGZyb20gT25lTWUuCgoKCk9uZU1lIExMQy4KUC5PLiBCb3ggMjI1NzIKSG91c3RvbiBUWCA3NzIyNw==",
                "contentType": "text/plain",
                "@data.type": "#microsoft.graph.fileAttachment"
            },
            {
                "name": "attachment-2.txt",
                "contentBytes": "QXR0YWNobWVudCBUd28gLQpUaGlzIGlzIGEgdGVzdCBzZW5kIGZyb20gT25lTWUuCgoKCk9uZU1lIExMQy4KUC5PLiBCb3ggMjI1NzIKSG91c3RvbiBUWCA3NzIyNw==",
                "contentType": "text/plain",
                "@data.type": "#microsoft.graph.fileAttachment"
            }
        ],
        "subject": "2023-08-30 10:35:17.366 - Microsoft Graph Email with Attachment Test",
        "bccRecipients": [],
        "ccRecipients": [],
        "body": {
            "contentType": "Text",
            "content": "This is the test email body."
        },
        "hasAttachments": true
    }
}

Response:

{"error": {
    "code": "RequestBodyRead",
    "message": "The property 'contentBytes' does not exist on type 'microsoft.graph.attachment'. Make sure to only use property names that are defined by the type or mark the type as open type."
}}

The error message is correct that contentBytes is not a property of microsoft.graph.attachment. However, the type that is being sent is microsoft.graph.fileAttachment. fileAttachment resource type extends attachment and clearly does have the property as detailed here in the reference fileAttachment resource type.

And contentBytes is in the example JSON from here user: sendMail.

POST https://graph.microsoft.com/v1.0/me/sendMail
Content-type: application/json

{
  "message": {
    "subject": "Meet for lunch?",
    "body": {
      "contentType": "Text",
      "content": "The new cafeteria is open."
    },
    "toRecipients": [
      {
        "emailAddress": {
          "address": "[email protected]"
        }
      }
    ],
    "attachments": [
      {
        "@odata.type": "#microsoft.graph.fileAttachment",
        "name": "attachment.txt",
        "contentType": "text/plain",
        "contentBytes": "SGVsbG8gV29ybGQh"
      }
    ]
  }
}

Other obvious problems with the examples show unencoded byte string or even show decoding an unencoded byte string for the contentBytes. The fileAttachment resource docs state "Make sure to encode the file content in base64 before assigning it to contentBytes."

Is this just a long standing unresolved bug? Surely we can't be the only ones using HTTP?


Solution

  • With the attachment problem in your example you have used @data.type

           {
                "name": "attachment-1.txt",
                "contentBytes": "QXR0YWNobWVudCBPbmUgLQpUaGlzIGlzIGEgdGVzdCBzZW5kIGZyb20gT25lTWUuCgoKCk9uZU1lIExMQy4KUC5PLiBCb3ggMjI1NzIKSG91c3RvbiBUWCA3NzIyNw==",
                "contentType": "text/plain",
                "@data.type": "#microsoft.graph.fileAttachment"
            },
    

    where it should be @odata.type so using your example with that correction

    {
    "saveToSentItems": "false",
    "message": {
        "toRecipients": [
            {
                "emailAddress": {
                    "address": "[email protected]"
                }
            }
        ],
        "attachments": [
            {
                "name": "attachment-1.txt",
                "contentBytes": "QXR0YWNobWVudCBPbmUgLQpUaGlzIGlzIGEgdGVzdCBzZW5kIGZyb20gT25lTWUuCgoKCk9uZU1lIExMQy4KUC5PLiBCb3ggMjI1NzIKSG91c3RvbiBUWCA3NzIyNw==",
                "contentType": "text/plain",
                "@odata.type": "#microsoft.graph.fileAttachment"
            },
            {
                "name": "attachment-2.txt",
                "contentBytes": "QXR0YWNobWVudCBUd28gLQpUaGlzIGlzIGEgdGVzdCBzZW5kIGZyb20gT25lTWUuCgoKCk9uZU1lIExMQy4KUC5PLiBCb3ggMjI1NzIKSG91c3RvbiBUWCA3NzIyNw==",
                "contentType": "text/plain",
                "@odata.type": "#microsoft.graph.fileAttachment"
            }
        ],
        "subject": "2023-08-30 10:35:17.366 - Microsoft Graph Email with Attachment Test",
        "bccRecipients": [],
        "ccRecipients": [],
        "body": {
            "contentType": "Text",
            "content": "This is the test email body."
        },
        "hasAttachments": true
    }
    }
    

    works fine. The attachment should be Base64 encoded, the documentation for the Graph is pretty patchy when you see an obvious error (like the Java example in this case) its best to try create an issue on GitHub and ask them to fix it so it helps others in the future.