Gmail API replies appearing as separate messages instead of threaded responses

Issue with Gmail API Threading

I’m working on a project where I need to send replies to existing email conversations using the Gmail API. The problem is that my replies keep showing up as individual messages rather than being properly grouped with the original email thread.

I can see that the messages do get associated with the correct conversation when I look at the thread view, but they don’t display as proper replies in the Gmail interface.

My Current Approach

I’ve been trying several things to fix this:

Setting up the thread connection:

emailObject.ThreadId = conversationId;

Adding reply headers:

mailMessage.Headers.Add("In-Reply-To", originalMessageId);
mailMessage.Headers.Add("References", originalMessageId);

Keeping the subject consistent:

mailMessage.Subject = originalEmail.Subject;

Code Example

Here’s the method I’m using to prepare the message:

private string PrepareReplyMessage(string encodedMimeData, string conversationId, string originalMessageId)
{
    var mailMessage = MimeKit.MimeMessage.Load(new MemoryStream(Convert.FromBase64String(encodedMimeData.Replace('-', '+').Replace('_', '/'))));
    mailMessage.Subject = parentSubject;

    mailMessage.Headers.Add("In-Reply-To", originalMessageId);
    mailMessage.Headers.Add("References", originalMessageId);

    using (var stream = new MemoryStream())
    {
        mailMessage.WriteTo(stream);
        return Convert.ToBase64String(stream.ToArray())
            .Replace('+', '-')
            .Replace('/', '_')
            .Replace("=", "");
    }
}

Sending the reply:

gmailMessage.Raw = preparedMime;
gmailMessage.ThreadId = conversationId;

UsersResource.MessagesResource.SendRequest sendRequest = _gmailService.Users.Messages.Send(gmailMessage, userEmail);
gmailMessage = await sendRequest.ExecuteAsync();

Even with all these steps, the Gmail interface still treats my replies as new messages. They show up in the same thread when I expand the conversation, but they don’t look like proper threaded replies.

What could I be missing here? Are there other headers or settings I need to configure to make Gmail recognize these as genuine replies?

same problem here! your ‘in-reply-to’ and ‘references’ headers need the full msg-id with angle brackets - without them, threading breaks. also check that your threadId matches exactly what gmail returns. hope this helps!

Had this exact problem last year building an email automation system. Gmail’s really picky about Message-ID format - it needs to follow RFC standards perfectly. Your headers look fine, but double-check you’re using the actual Message-ID from the original email, not some random identifier. It should have the full string with angle brackets like <[email protected]>. Also make sure you’re not messing with the subject line - keep the original prefix exactly as is. What finally fixed it for me was setting the Date header properly and making sure the From address matched the authenticated sender. Gmail’s threading is super strict - mess up even one detail and your message won’t thread right.

Gmail API threading problems usually boil down to grabbing the wrong Message-ID. You’re probably pulling Gmail’s internal message ID instead of the actual SMTP Message-ID header - they’re totally different things. Gmail’s ID is just for their system, but the Message-ID header is what handles threading. Pull the Message-ID from the headers collection, not the API response metadata. Also watch out for the References header. If the original email was already threaded, you need the entire References chain with all previous Message-IDs, not just the immediate parent. Include the complete conversation history.