Create a new (send/received) message with itemAttachment using Microsoft Graph

Hielke Hoeve 0 Reputation points
2024-11-18T08:05:16.04+00:00

Because Microsoft is phasing out EWS in a rather unusual fashion I am rewriting all of my code to use Microsoft Graph. Some things are quite easy and neat to rewrite. Copying a Message is not one of them. It has been a dreadful process of trial and error...

What I am trying to do:

I'm trying to create a new message in a users mailbox with a message as attachment. I don't want to actually send the message, I just want to simulate that the message was send/received. For this I am using POST /me/messages + some SingleValueExtendedProperties so the message is not a draft. This is different from all other questions where people want to use POST /me/sendMail like:

My HTTP requests:

Creating the message (without an attachment) gives an HTTP 202. The message is shown in Outlook exactly as expected:

POST https://graph.microsoft.com/v1.0/me/messages HTTP/1.1
authorization: bearer {access_token}
content-type: application/json

{
  "createdDateTime": "2024-11-12T08:49:36Z",
  "lastModifiedDateTime": "2024-11-12T08:49:36Z",
  "receivedDateTime": "2024-11-12T08:49:36Z",
  "sentDateTime": "2024-11-12T08:49:36Z",
  "hasAttachments": true,
  "subject": "Root e-mail",
  "body": "Root e-mail",
  "parentFolderId": "X",
  "sender": {
    "emailAddress": {
      "name": "Hielke",
      "address": "hielke@example.com"
    }
  },
  "from": {
    "emailAddress": {
      "name": "Hielke",
      "address": "hielke@example.com"
    }
  },
  "toRecipients": [
    {
      "emailAddress": {
        "name": "Hielke",
        "address": "hielke@example.com"
      }
    }
  ],
  "SingleValueExtendedProperties": [
    {
      "Id": "Integer 0x0E07",
      "Value": "1"
    },
    {
      "Id": "SystemTime 0x0039",
      "Value": "2024-11-12T08:49:36Z"
    },
    {
      "Id": "SystemTime 0x0E06",
      "Value": "2024-11-12T08:49:36Z"
    }
  ]
}

When adding an Attachment I am using the content of /me/messages/Y. When POSTing the follow request it returns an HTTP 202, so it works, but creates it as a draft unlike the root message.

POST https://graph.microsoft.com/v1.0/me/messages/X/attachments HTTP/1.1
authorization: bearer {access_token}
content-type: application/json

{
    "@odata.type": "#microsoft.graph.itemAttachment",
    "name": "Y",
    "item": {
        "@odata.type": "#microsoft.graph.message",
        "@odata.etag": "Y",
        "id": "Y",
        "createdDateTime": "2024-11-08T12:38:56Z",
        "lastModifiedDateTime": "2024-11-08T12:38:59Z",
        "changeKey": "Y",
        "categories": [],
        "receivedDateTime": "2024-11-08T12:38:57Z",
        "sentDateTime": "2024-11-08T12:38:53Z",
        "hasAttachments": true,
        "internetMessageId": "Y",
        "subject": "E-mail as attachment",
        "importance": "normal",
        "parentFolderId": "Y",
        "conversationId": "Y",
        "conversationIndex": "Y",
        "isDeliveryReceiptRequested": false,
        "isReadReceiptRequested": false,
        "isRead": true,
        "isDraft": false,
        "webLink": "https://outlook.office365.com/owa/?ItemID=X&exvsurl=1&viewmodel=ReadMessageItem",
        "inferenceClassification": "focused",
        "body": {
            "contentType": "html",
            "content": "E-mail as attachment"
        },
        "sender": {
            "emailAddress": {
                "name": "Hielke",
                "address": "hielke@example.com"
            }
        },
        "from": {
            "emailAddress": {
                "name": "Hielke",
                "address": "hielke@example.com"
            }
        },
        "toRecipients": [
            {
                "emailAddress": {
                    "name": "Hielke",
                    "address": "hielke@example.com"
                }
            }
        ],
        "ccRecipients": [],
        "bccRecipients": [],
        "replyTo": [],
        "flag": {
            "flagStatus": "notFlagged"
        },
        "SingleValueExtendedProperties": [
            {
                "Id": "Integer 0x0E07",
                "Value": "1"
            },
            {
                "Id": "SystemTime 0x0039",
                "Value": "2024-11-08T12:38:57Z"
            },
            {
                "Id": "SystemTime 0x0E06",
                "Value": "2024-11-08T12:38:57Z"
            }
        ]
    }
}

Creating a message with a FileAttachment directly works as expected. Creating a message with an ItemAttachment directly works (HTTP 202), but also created the attachment is a draft.

And then I haven't even tried creating an ItemAttachment with an ItemAttachment (inception)...

What I have tried:

  • Adding the SingleValueExtendedProperties to the Item of ItemAttachment as well does not make the ItemAttachment a non-draft.
  • Passing ALL properties of the Message I want to attach does not make the ItemAttachment a non-draft. It does not show the any of the timestamps of the Message.
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,480 questions
Microsoft Exchange
Microsoft Exchange
Microsoft messaging and collaboration software.
592 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jake Zhang-MSFT 7,315 Reputation points Microsoft Vendor
    2024-11-19T09:17:18.34+00:00

    Hi @Hielke Hoeve ,

    Welcome to the Microsoft Q&A platform!

    It sounds like you're facing quite a challenge transitioning from EWS to Microsoft Graph, especially when creating messages that include other messages as attachments without marking them as drafts. Let's try to step through this.

    Based on your description, it seems like the main issue is that the attached message is being created as a draft. Here are some suggestions that may help:

    1. Make sure that the SingleValueExtendedProperties for both the root message and the attached message are set correctly. It looks like you've already tried this, but double-checking the property IDs and values ​​may help.
    2. Make sure you're using the correct endpoints for creating messages and adding attachments. Your current approach seems correct, but here's a quick recap:
    • Use POST /me/messages to create the root message.
    • Use POST /me/messages/{message-id}/attachments to add attachments.
    1. When creating the attached message, explicitly set the isDraft property to false. This may help ensure that the message is not marked as a draft.
    2. Make sure that all required properties for the attached message are set correctly. Sometimes, missing properties can cause unexpected behavior.

    Here is an example of how you might construct a request:

    Create a root message

    POST https://graph.microsoft.com/v1.0/me/messages
    Authorization: Bearer {access_token}
    Content-Type: application/json
    
    {
      "createdDateTime": "2024-11-12T08:49:36Z",
      "lastModifiedDateTime": "2024-11-12T08:49:36Z",
      "receivedDateTime": "2024-11-12T08:49:36Z",
      "sentDateTime": "2024-11-12T08:49:36Z",
      "hasAttachments": true,
      "subject": "Root e-mail",
      "body": {
        "contentType": "text",
        "content": "Root e-mail"
      },
      "parentFolderId": "X",
      "sender": {
        "emailAddress": {
          "name": "Hielke",
          "address": "hielke@example.com"
        }
      },
      "from": {
        "emailAddress": {
          "name": "Hielke",
          "address": "hielke@example.com"
        }
      },
      "toRecipients": [
        {
          "emailAddress": {
            "name": "Hielke",
            "address": "hielke@example.com"
          }
        }
      ],
      "singleValueExtendedProperties": [
        {
          "id": "Integer 0x0E07",
          "value": "1"
        },
        {
          "id": "SystemTime 0x0039",
          "value": "2024-11-12T08:49:36Z"
        },
        {
          "id": "SystemTime 0x0E06",
          "value": "2024-11-12T08:49:36Z"
        }
      ]
    }
    

    Add an ItemAttachment

    POST https://graph.microsoft.com/v1.0/me/messages/{message-id}/attachments
    Authorization: Bearer {access_token}
    Content-Type: application/json
    
    {
      "@odata.type": "#microsoft.graph.itemAttachment",
      "name": "Attached Message",
      "item": {
        "@odata.type": "#microsoft.graph.message",
        "subject": "E-mail as attachment",
        "body": {
          "contentType": "html",
          "content": "E-mail as attachment"
        },
        "sender": {
          "emailAddress": {
            "name": "Hielke",
            "address": "hielke@example.com"
          }
        },
        "from": {
          "emailAddress": {
            "name": "Hielke",
            "address": "hielke@example.com"
          }
        },
        "toRecipients": [
          {
            "emailAddress": {
              "name": "Hielke",
              "address": "hielke@example.com"
            }
          }
        ],
        "isDraft": false,
        "singleValueExtendedProperties": [
          {
            "id": "Integer 0x0E07",
            "value": "1"
          },
          {
            "id": "SystemTime 0x0039",
            "value": "2024-11-08T12:38:57Z"
          },
          {
            "id": "SystemTime 0x0E06",
            "value": "2024-11-08T12:38:57Z"
          }
        ]
      }
    }
    

    Please feel free to contact me for any updates. And if this helps, don't forget to mark it as an answer.

    Best,

    Jake Zhang


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.