Reply to a message in its existing Gmail thread
See more GMail REST API Examples
Live send. Supply the Gmail ID of a received message and use an authorized From address. Fetch its raw MIME so Chilkat.Email can parse encoded headers and recipient addresses. Send a plain-text reply to Reply-To, falling back to From. The reply goes to the original sender or reply address.
The request supplies Gmail’s threadId and matching Subject. The MIME In-Reply-To contains the original RFC Message-ID. References preserves the conversation ancestry and appends that Message-ID. Select a message with a valid Message-ID and review its recipient before running this sending example. Authorize both gmail.readonly and gmail.send, or use gmail.modify as shown below.
Authorize the application with https://www.googleapis.com/auth/gmail.modify. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
Chilkat Unicode C++ Downloads
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkStringBuilderW.h>
#include <CkBinDataW.h>
#include <CkEmailW.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkJsonObjectW token;
success = token.LoadFile(L"gmail-token.json");
if (success != true) {
wprintf(L"Could not read gmail-token.json from the private working directory.\n");
return;
}
if ((token.JsonTypeOf(L"access_token") != 1) || (token.StringOfEquals(L"access_token",L"",true) == true)) {
wprintf(L"An OAuth access token is required.\n");
return;
}
CkHttpW http;
http.put_RequireSslCertVerify(true);
http.put_AuthToken(token.stringOf(L"access_token"));
CkHttpResponseW resp;
CkJsonObjectW reply;
CkStringBuilderW url;
url.Append(L"https://gmail.googleapis.com/gmail/v1/users/me/messages/");
url.Append(L"GMAIL_MESSAGE_ID");
url.Append(L"?format=raw");
success = http.HttpNoBody(L"GET",url.getAsString(),resp);
if (resp.get_StatusCode() == 0) {
wprintf(L"Gmail did not return an HTTP response.\n");
return;
}
if ((success != true) || (resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
wprintf(L"Gmail HTTP %d\n",resp.get_StatusCode());
success = reply.Load(resp.bodyStr());
if (success == true) {
wprintf(L"%s\n",reply.stringOf(L"error.message"));
}
return;
}
success = reply.Load(resp.bodyStr());
if (success != true) {
wprintf(L"Expected a Gmail JSON response.\n");
return;
}
if ((reply.JsonTypeOf(L"threadId") != 1) || (reply.StringOfEquals(L"threadId",L"",true) == true)) {
wprintf(L"Expected threadId in the response.\n");
return;
}
if ((reply.JsonTypeOf(L"raw") != 1) || (reply.StringOfEquals(L"raw",L"",true) == true)) {
wprintf(L"Expected raw in the response.\n");
return;
}
const wchar_t *threadId = reply.stringOf(L"threadId");
CkBinDataW originalMime;
success = reply.BytesOf(L"raw",L"base64url",originalMime);
if (success != true) {
wprintf(L"%s\n",reply.lastErrorText());
return;
}
CkEmailW original;
success = original.SetFromMimeBd(originalMime);
if (success != true) {
wprintf(L"%s\n",original.lastErrorText());
return;
}
const wchar_t *messageIdHeader = original.getHeaderField(L"Message-ID");
if ((original.get_LastMethodSuccess() != true) || (messageIdHeader == L"")) {
wprintf(L"The original message needs a Message-ID for this threaded reply.\n");
return;
}
CkEmailW email;
email.put_From(L"Sender <sender@example.com>");
// Use the parsed Reply-To mailbox list when present, otherwise use From.
const wchar_t *replyAddress = original.replyTo();
if (replyAddress == L"") {
replyAddress = original.ck_from();
}
success = email.AddMultipleTo(replyAddress);
if (success != true) {
wprintf(L"%s\n",email.lastErrorText());
return;
}
if (email.get_NumTo() == 0) {
wprintf(L"The original message has no usable reply address.\n");
return;
}
email.put_Subject(original.subject());
email.put_Charset(L"utf-8");
email.put_Body(L"Thank you for your message.");
email.AddHeaderField(L"In-Reply-To",messageIdHeader);
CkStringBuilderW references;
const wchar_t *previousReferences = original.getHeaderField(L"References");
if ((original.get_LastMethodSuccess() == true) && (previousReferences != L"")) {
references.Append(previousReferences);
references.Append(L" ");
}
references.Append(messageIdHeader);
email.AddHeaderField(L"References",references.getAsString());
CkBinDataW mime;
success = email.GetMimeBd(mime);
if (success != true) {
wprintf(L"%s\n",email.lastErrorText());
return;
}
CkJsonObjectW request;
success = request.UpdateBd(L"raw",L"base64url",mime);
if (success != true) {
wprintf(L"%s\n",request.lastErrorText());
return;
}
success = request.UpdateString(L"threadId",threadId);
if (success != true) {
wprintf(L"%s\n",request.lastErrorText());
return;
}
success = http.HttpJson(L"POST",L"https://gmail.googleapis.com/gmail/v1/users/me/messages/send",request,L"application/json",resp);
if (resp.get_StatusCode() == 0) {
wprintf(L"Gmail did not return an HTTP response.\n");
return;
}
if ((success != true) || (resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
wprintf(L"Gmail HTTP %d\n",resp.get_StatusCode());
success = reply.Load(resp.bodyStr());
if (success == true) {
wprintf(L"%s\n",reply.stringOf(L"error.message"));
}
return;
}
success = reply.Load(resp.bodyStr());
if (success != true) {
wprintf(L"Expected a Gmail JSON response.\n");
return;
}
if ((reply.JsonTypeOf(L"id") != 1) || (reply.StringOfEquals(L"id",L"",true) == true)) {
wprintf(L"Expected id in the response.\n");
return;
}
wprintf(L"Sent reply ID: %s\n",reply.stringOf(L"id"));
wprintf(L"Thread ID: %s\n",reply.stringOf(L"threadId"));
}