I am trying to implement a feature to reply to an email thread using the Gmail API. Despite my efforts, the replies are being sent as new messages instead of being threaded under the original email. However, they do appear in the correct thread in the overall conversation view.
Here is what I have done so far:
Setting the Thread ID:
csharp
“gmailMessage.ThreadId = threadId;”
Adding the Message ID of the Email Being Replied to:
csharp
“mimeMessage.Headers.Add(“In-Reply-To”, msgId);”
“mimeMessage.Headers.Add(“References”, msgId);”
Ensuring the Subject is the Same:
csharp
“mimeMessage.Subject = mailMsg.Subject”
Despite these steps, the Gmail UI still shows the reply as a new message, though it is recorded in the same thread internally.
Code Snippet for Reference:
csharp
“private string AddThreadIdToMime(string base64RawMime, string threadId, string msgId)
{
var mimeMessage = MimeKit.MimeMessage.Load(new MemoryStream(Convert.FromBase64String(base64RawMime.Replace(‘-‘, ‘+’).Replace(‘_’, ‘/’))));
mimeMessage.Subject = originalSubject;
mimeMessage.Headers.Add("In-Reply-To", msgId);
mimeMessage.Headers.Add("References", msgId);
using (var memory = new MemoryStream())
{
mimeMessage.WriteTo(memory);
return Convert.ToBase64String(memory.ToArray())
.Replace('+', '-')
.Replace('/', '_')
.Replace("=", "");
}
}”
Sending the message:
gmailMsg.Raw = rawMime;
gmailMsg.ThreadId = threadId;
UsersResource.MessagesResource.SendRequest msgSendReq = _gmailService.Users.Messages.Send(gmailMsg, userEmail);
gmailMsg = await msgSendReq.ExecuteAsync();
}”
Question:
What am I missing? Is there any additional step required to ensure that the reply is correctly threaded in the Gmail UI?
Thank you for your help!