2

I allow users to send emails with their Outlook account by using Microsoft Graph API, but it seems to be creating multiple threads on the other side.

When using Mailgun API to send the user emails, I am able to pass the In-Reply-To message header that references the previous message Message-ID, and threads are clustered properly by clients on the other side (Outlook/Gmail etc)

But when using Microsoft Graph API I try to pass the In-Reply-To and it is not accepted by the API

graph_url = 'https://graph.microsoft.com/v1.0'

headers = {
    'User-Agent': 'api/1.0',
    'Authorization': f'Bearer {outlook_token}',
    'Accept': 'application/json',
    'Content-Type': 'application/json'
}

# Create recipient list in required format.
recipient_list = [{'emailAddress': {'name': name, 'address': address}} for name, address in recipients]
reply_to_list = [{'emailAddress': {'name': name, 'address': address}} for name, address in reply_to]

# Create email message in required format.
email_object = {
    'message': {
        'subject': subject,
        'body': {
            'contentType': content_type,
            'content': body
        },
        'toRecipients': recipient_list,
        'replyTo': reply_to_list,
        'attachments': [{
            '@odata.type': '#microsoft.graph.fileAttachment',
            'contentBytes': b64_content.decode('utf-8'),
            'contentType': mime_type,
            'name': file.name
        }],
        'internetMessageHeaders': [
            {
                "name": "In-Reply-To",
                "value": in_reply_to
            },
        ]
    },
    'saveToSentItems': 'true'
}

# Do a POST to Graph's sendMail API and return the response.
request_url = f'{graph_url}/me/microsoft.graph.sendMail'

response = requests.post(url=request_url, headers=headers, json=email_object)

https://learn.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0

I get the following response:

{
    "error": {
        "code": "InvalidInternetMessageHeader",
        "message": "The internet message header name 'in-Reply-To' should start with 'x-' or 'X-'.",
        "innerError": {
            "request-id": "7f82b9f5-c345-4744-9f21-7a0e9d75cb67",
            "date": "2019-05-03T04:09:43"
        }
    }
}

Is there any way of having the emails sent in the same thread for recipients clients?

2 Answers2

6

Even if this thread is old, maybe it will help someone:

With the latest graph version you can now also send e-mails with MIME content. With this API you can set the in-reply-to header and other header fields, which was not possible with the JSON upload.

onestarblack
  • 774
  • 7
  • 21
4

You can't manipulate standard message headers this way. The internetMessageHeaders collection will only accept "custom headers". These are message headers that begin with x- i.e. x-some-custom-header).

In order to reply to a message, you need to use the /createReply endpoint:

POST https://graph.microsoft.com/v1.0/me/messages/{id-of-message}/createReply

This will generate a message with the appropriate headers. You can then update this message to add additional content/attachments before you send it:

PATCH https://graph.microsoft.com/v1.0/me/messages/{id-of-reply}

{
  "body": {
    "contentType": "HTML",
    "content": body
  }
}

POST https://graph.microsoft.com/v1.0/me/messages/{id-of-reply}/send
Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
  • I think my company implemented replying to emails before these APIs were developed. For the Graph API, we have existing user permissions for Mail.Read and Mail.Send. Is there are way to still send email replies with those permissions? We think that there may have been a recent internal access change, and this is impacting how our customers are replying to emails w/in our app. Thank you! – dot_zero Sep 25 '19 at 00:57
  • If that's not possible, we may need to request additional permissions from our users. That could be a highly disruptive process, so we'd like to avoid it if possible. – dot_zero Sep 25 '19 at 00:57
  • Looks like `/reply` now supports the `message` field: https://stackoverflow.com/a/58546651/2204868 – dot_zero Oct 24 '19 at 17:45
  • Hy @Marc Thanks for the answer, it clear my some of doubts regarding reply, but can you please help me to to identify that what is `{id-of-message}` and what is `{id-of-reply}`? – HarisH Sharma Jan 06 '20 at 12:48
  • @Marc LaFleur: Following your suggestion, I was able to make the In-Reply-To header work. However, I was not able to update the message to send attachments. I used the same com.microsoft.graph.models.Message object for the sendMail to send attachments. What special things should be done with Update? – Lin Chen Apr 20 '22 at 18:45