Graph API sendMail - The property 'contentBytes' does not exist on type 'microsoft.graph.attachment'.

Ted Spradley 5 Reputation points
2023-08-30T20:39:28.0433333+00:00

We are using HTTP for user: sendMail following the example at https://learn.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=http:

The error message correctly asserts that

The property 'contentBytes' does not exist on type 'microsoft.graph.attachment'.

However, the data type in the Mail JSON object is fileAttachment, not attachment. The reference for fileAttachment clearly shows "contentBytes" as a property "fileAttachment resource type". Am I missing something? Thank you.

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."
}}

HTTP POST

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

{
    "saveToSentItems": "false",
    "message": {
        "toRecipients": [
            {"emailAddress": {"address": "example1@example.com"}},
            {"emailAddress": {"address": "example2@example.com"}}
        ],
        "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 15:23:30.843 - Microsoft Graph Email with Attachment Test",
        "bccRecipients": [],
        "ccRecipients": [],
        "body": {
            "contentType": "Text",
            "content": "This is the test email body."
        },
        "hasAttachments": true
    }
}
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,538 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Ted Spradley 5 Reputation points
    2023-08-31T13:23:07.04+00:00

    The issue was missing the 'o' in the key ""@data.type" in the key:value pair "@data.type": "#microsoft.graph.fileAttachment". The correct key is "@odata.type" so k:v -> "@odata.type": "#microsoft.graph.fileAttachment"

    Credit StackOverflow https://stackoverflow.com/questions/77010710/ms-graph-api-error-contentbytes-not-a-property-of-type-microsoft-graph-attachm/77012257#77012257

            "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"
                }
            ],
    
    1 person found this answer helpful.
    0 comments No comments

  2. Ab-8756 805 Reputation points
    2023-08-31T19:01:49.12+00:00

    Hello Ted Spradley,
    Thank you for reaching out to this Q&A Forum.
    When creating a file attachment, the correct format is "@odata.type": "#microsoft.graph.fileAttachment"
    which you need to add in the request body. Please replace your body to;-

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

    Hope this helps.

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


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.