Gmail API thread retrieval missing reply messages when original email sent programmatically

My Setup:

I’m working with two email accounts - let’s call them User X and User Y. They’re on separate Google Workspace domains.

What I’m Doing:

  1. I send an email from User X to User Y using Gmail API through Google Apps Script.
  2. User Y replies to this email using regular Gmail interface.
  3. I try to fetch all messages in the thread using the thread ID.

My Code:

function createAndSendMessage(targetEmail, emailSubject, messageBody) {
  
  // Build the email message
  var messageContent = [
    'To: ' + targetEmail,
    'Subject: ' + emailSubject,
    '',
    messageBody
  ].join('\r\n');

  var encodedMessage = Utilities.base64EncodeWebSafe(messageContent);

  // Send via Gmail API
  var response = Gmail.Users.Messages.send({
    raw: encodedMessage
  }, 'me');

  return response.threadId;
}

The Issue:

When I use the thread ID to get all messages in the conversation, the API only returns the original message I sent programmatically. The reply from User Y doesn’t show up in the thread, even though it appears correctly threaded in the Gmail web interface.

My Question:

What do I need to include in my initial email to make sure replies get properly threaded when I retrieve them via the API? Are there specific headers I should be setting?

I’ve checked that the reply is visible in Gmail UI and shows as part of the same conversation, but the API call to get thread messages only returns one message instead of both.

Had this exact problem 6 months ago building something similar. Gmail’s threading algorithm relies heavily on the Message-ID header for API operations, even though the web interface is more flexible with matching. You’re missing the Message-ID header when constructing the raw message. When you send via API without it, Gmail auto-generates one, but it doesn’t always thread replies properly for programmatic retrieval. Add these headers to your messageContent array: var messageId = ‘<’ + Utilities.getUuid() + ‘@yourdomain.com>’; var messageContent = [ 'Message-ID: ’ + messageId, ‘To: ’ + targetEmail, ‘Subject: ’ + emailSubject, ‘’, messageBody ].join(’\r\n’); Also add References and In-Reply-To headers if this is part of an ongoing conversation. After this change, replies should show up correctly when you fetch thread messages via the API.

also check ur pulling from the right account. the threadId might get created on the sender’s side, but the reply could end up in a different thread on the receiver’s end. i’ve seen this with cross-domain workspace setups where api permissions dont sync between domains. pull the thread from both user accounts to see where the reply actually landed.

Had the exact same problem integrating with our CRM last year. It’s not just the Message-ID header - Gmail’s threading for API-sent emails is way different from what you see in the web interface. The web version uses smarter matching algorithms that aren’t available through API calls.

You need proper MIME structure in your raw message. Wrap your content with the right MIME headers - Content-Type and MIME-Version. Gmail API is super picky about line endings too, so make sure you’re using CRLF instead of just LF.

Adding a proper Date header helped me a ton. Gmail sometimes groups threads by time, so that matters for thread association. After I fixed all this stuff, our reply detection jumped from 60% to almost 100%.

The UI vs API threading difference has been a known issue for years - really annoying but that’s just how it works.