I'm getting different threading on sender and recipient side using gmail api

55 views Asked by At

Details about the problem: I'm trying to reply to a specific message using "In-Reply-To" and "References", so both receiver and sender can see emails in the same thread.

For this I'm using:

[POST https://gmail.googleapis.com/upload/gmail/v1/users/{userId}/messages/send]

(https://developers.google.com/gmail/api/reference/rest/v1/users.messages/send) and 'message/rfc822' format.

Here is how I build the email and send it: Firstly I get messageId by threadId using the api url:

$"https://www.googleapis.com/gmail/v1/users/me/messages/{**threadId**}?format=metadata&metadataHeaders=In-Reply-To&metadataHeaders=References&metadataHeaders=Message-ID&metadataHeaders=Subject&fields=payload%2Fheaders";

Then I construct the email:

var email = FormatEmail(user, to, subject, messageHtml, attachments, **messageId**);

FormatEmail function:

{
            string subjectBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(subject));
            string content =
                $"Content-Type: multipart/mixed; boundary=\"foo_bar_baz\"\r\n" +
                "MIME-Version: 1.0\r\n" +
                "from: " + $"{user.fullName} <{user.email}>\r\n" +
                $"to: {to}\r\n" +
               ** $"In-Reply-To: {messageId}\r\n" +**
               ** $"References: {messageId}\r\n" +**
               ** "subject: " +
                $"=?UTF-8?B?{subjectBase64}?=" +**
                "\n\n" +
                "--" +
                foo_bar_baz +
                "\r\n" +
                "Content-Type: text/html; charset=\"UTF-8\"\r\n" +
                "MIME-Version: 1.0\r\n" +
                "Content-Transfer-Encoding: 7bit\r\n\r\n" +
                $"{message}\r\n\r\n" +
                AddAttachments(attachments) +
                "--" +
                foo_bar_baz +
                "--";
            return content;
        }

Then I send it:

var url = $"https://www.googleapis.com/upload/gmail/v1/users/" + Email + "/messages/send?uploadType=multipart";

var request = new HttpRequestMessage(HttpMethod.Post, url)
                {
                    Headers =
                        {
                            { HeaderNames.Authorization, $"Bearer {AccessToken}" },
                        },
                    Content = new StringContent(email, Encoding.UTF8, "message/rfc822")
                };

!!Problem

When I'm using the approach above I get unexpected behavior: The sender sees each email in a different thread, while the recipient sees them in the same thread (as I want for both of them) Sender gmail ui view: Sender mail ui as each message in a separate thread. 3 emails Recipient gmail ui view:

  1. last email in thread 2.opened thread with 3 emails

BUT In my default macOS Mail UI it looks right on the sender side: correctly threaded 3 messages

What I already tried:

  1. Official docs of Google says to specify threadId in Message body: docs screenshot of docs So I tried to add it in various places of my email template, but it didn't work for me.

  2. Also I found this on stack overflow: [https://stackoverflow.com/questions/33171769/gmail-api-in-reply-to-not-workinggoogle-not-handling-on-one-side/33177031#33177031](Gmail API In-Reply-To not working(Google not handling on one side)

    not-workinggoogle-not-handling-on-one-side/33177031#33177031) and I was trying to reproduce the same structure with my template for emails and when I replicated it the API was throwing an error that it can't recognize the recipient address somehow.

    Here is the code for that try:

    string content = $"Content-Type: multipart/mixed; boundary="foo_bar_baz"\r\n" +

     "--" +
     foo_bar_baz +
     "\r\n" +
     "Content-Type: application/json; charset=UTF-8\r\n" +
     "\r\n" +
     "{\r\n" +
     $"  \"threadId\": \"{threadId}\"\r\n" +
     "}\r\n" +
     "--" +
     foo_bar_baz +
     "\r\n" +
    
     "Content-Type: message/rfc822" +
     "\r\n" +
     $"Content-Type: multipart/mixed; boundary=\"foo_bar\"" +
     "\r\n" +
     "from: " + $"{user.fullName} <{user.email}>\r\n" +
     $"to: {to}\r\n" +
     "subject: " +
     $"=?UTF-8?B?{subjectBase64}?=" +
     "\n\n" +
    
     $"--foo_bar\r\n" +
     "Content-Type: text/html; charset=\"UTF-8\"\r\n" +
     "MIME-Version: 1.0\r\n" +
     "Content-Transfer-Encoding: 7bit\r\n\r\n" +
     $"{message}\r\n\r\n" +
     AddAttachments(attachments) +
     "--foo_bar--\r\n" +
     $"--foo_bar_baz--\r\n";
    

    return content;

0

There are 0 answers