Microsoft Graph API mail message: Is there any option to create inbox message from a MIME, NOT as Draft?

Viewed 278

I am Using Microsoft Graph REST API 1.0, trying to create a new message (not draft) from a MIME but I came into some issues.

I managed to create a message using a base64 encoded MIME but it gets flagged as a draft (isDraft=true) (https://docs.microsoft.com/en-us/graph/api/user-post-messages?view=graph-rest-1.0&tabs=http)

According to multiple sources isDraft flag can only be set at message creation so I take the previously created message, change isDraft flag to false using a SingleValueLegacyExtendedProperty and I POST it to the inbox
but the original MIME headers get modified (a lot of the original removed, some new created) and I need to keep them like the original.

Why MIME original headers are modified after change isDraft and respost the same message? Is there a way to avoid this behavior?

Also if there is another aproach to archive what I want, please let me know.

Thanks!

Some of my code below

// Create a new message (Original MIME)
newMessage = (Message) graphServiceClient
        .customRequest(String.format("/users/%s/messages/", USER_EMAIL), Message.class)
        .buildRequest(new HeaderOption("Content-Type", "text/plain"))
        .post(Base64.encodeBase64(IOUtils.toByteArray(
            Objects.requireNonNull(
                this.getClass().getClassLoader().getResourceAsStream(
                    MIME_MAIL_WITH_ATTACHMENTS)))));

//flag newMessage as draft (despite that newMessage has the original headers, after this update they become different from the original)                   
newMessage.singleValueExtendedProperties = setDraftFalse();

  private SingleValueLegacyExtendedPropertyCollectionPage setDraftFalse() {
    final SingleValueLegacyExtendedProperty prop = new SingleValueLegacyExtendedProperty();
    prop.id = "Integer 0x0E07";
    prop.value = "4";

    final SingleValueLegacyExtendedPropertyCollectionResponse response = new SingleValueLegacyExtendedPropertyCollectionResponse();
    response.value = new ArrayList<SingleValueLegacyExtendedProperty>();
    response.value.add(prop);
    return new SingleValueLegacyExtendedPropertyCollectionPage(
        response, null);
  }

// Post the message updated 
graphServiceClient.me().messages().buildRequest().post(newMessage);
2 Answers

Try to add X-Unsent: 0 to the message MIME headers.

I have figured out a hack that seems to be working. We use the graph draft APIs to convert MIME to JSON and submit it with singleValueExtendedProperties

There are 3 steps:

  1. POST /me/messages - Create a new draft with MIME.
  2. POST /me/mailFolders/{ID}/messages

As the input JSON. Get output from step 1 and add

"singleValueExtendedProperties": [
      {
         "id":"Integer 0x0E07",
         "value":"4"
      }
    ]
  1. DELETE /me/messages/{ID}

The ID is from step 1.

Related