Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.0.0+

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.

Google documentation

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
token: HCkJsonObject;
http: HCkHttp;
resp: HCkHttpResponse;
reply: HCkJsonObject;
url: HCkStringBuilder;
threadId: PWideChar;
originalMime: HCkBinData;
original: HCkEmail;
messageIdHeader: PWideChar;
email: HCkEmail;
replyAddress: PWideChar;
references: HCkStringBuilder;
previousReferences: PWideChar;
mime: HCkBinData;
request: HCkJsonObject;

begin
success := False;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

token := CkJsonObject_Create();
success := CkJsonObject_LoadFile(token,'gmail-token.json');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not read gmail-token.json from the private working directory.');
    Exit;
  end;
if ((CkJsonObject_JsonTypeOf(token,'access_token') <> 1) or (CkJsonObject_StringOfEquals(token,'access_token','',True) = True)) then
  begin
    Memo1.Lines.Add('An OAuth access token is required.');
    Exit;
  end;
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putAuthToken(http,CkJsonObject__stringOf(token,'access_token'));
resp := CkHttpResponse_Create();
reply := CkJsonObject_Create();
url := CkStringBuilder_Create();
CkStringBuilder_Append(url,'https://gmail.googleapis.com/gmail/v1/users/me/messages/');
CkStringBuilder_Append(url,'GMAIL_MESSAGE_ID');
CkStringBuilder_Append(url,'?format=raw');
success := CkHttp_HttpNoBody(http,'GET',CkStringBuilder__getAsString(url),resp);
if (CkHttpResponse_getStatusCode(resp) = 0) then
  begin
    Memo1.Lines.Add('Gmail did not return an HTTP response.');
    Exit;
  end;
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) < 200) or (CkHttpResponse_getStatusCode(resp) >= 300)) then
  begin
    Memo1.Lines.Add('Gmail HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
    success := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
    if (success = True) then
      begin
        Memo1.Lines.Add(CkJsonObject__stringOf(reply,'error.message'));
      end;
    Exit;
  end;
success := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
if (success <> True) then
  begin
    Memo1.Lines.Add('Expected a Gmail JSON response.');
    Exit;
  end;
if ((CkJsonObject_JsonTypeOf(reply,'threadId') <> 1) or (CkJsonObject_StringOfEquals(reply,'threadId','',True) = True)) then
  begin
    Memo1.Lines.Add('Expected threadId in the response.');
    Exit;
  end;
if ((CkJsonObject_JsonTypeOf(reply,'raw') <> 1) or (CkJsonObject_StringOfEquals(reply,'raw','',True) = True)) then
  begin
    Memo1.Lines.Add('Expected raw in the response.');
    Exit;
  end;
threadId := CkJsonObject__stringOf(reply,'threadId');
originalMime := CkBinData_Create();
success := CkJsonObject_BytesOf(reply,'raw','base64url',originalMime);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkJsonObject__lastErrorText(reply));
    Exit;
  end;
original := CkEmail_Create();
success := CkEmail_SetFromMimeBd(original,originalMime);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(original));
    Exit;
  end;
messageIdHeader := CkEmail__getHeaderField(original,'Message-ID');
if ((CkEmail_getLastMethodSuccess(original) <> True) or (messageIdHeader = '')) then
  begin
    Memo1.Lines.Add('The original message needs a Message-ID for this threaded reply.');
    Exit;
  end;
email := CkEmail_Create();
CkEmail_putFrom(email,'Sender <sender@example.com>');
//  Use the parsed Reply-To mailbox list when present, otherwise use From.
replyAddress := CkEmail__replyTo(original);
if (replyAddress = '') then
  begin
    replyAddress := CkEmail__from(original);
  end;
success := CkEmail_AddMultipleTo(email,replyAddress);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;
if (CkEmail_getNumTo(email) = 0) then
  begin
    Memo1.Lines.Add('The original message has no usable reply address.');
    Exit;
  end;
CkEmail_putSubject(email,CkEmail__subject(original));
CkEmail_putCharset(email,'utf-8');
CkEmail_putBody(email,'Thank you for your message.');
CkEmail_AddHeaderField(email,'In-Reply-To',messageIdHeader);
references := CkStringBuilder_Create();
previousReferences := CkEmail__getHeaderField(original,'References');
if ((CkEmail_getLastMethodSuccess(original) = True) and (previousReferences <> '')) then
  begin
    CkStringBuilder_Append(references,previousReferences);
    CkStringBuilder_Append(references,' ');
  end;
CkStringBuilder_Append(references,messageIdHeader);
CkEmail_AddHeaderField(email,'References',CkStringBuilder__getAsString(references));
mime := CkBinData_Create();
success := CkEmail_GetMimeBd(email,mime);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;
request := CkJsonObject_Create();
success := CkJsonObject_UpdateBd(request,'raw','base64url',mime);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkJsonObject__lastErrorText(request));
    Exit;
  end;
success := CkJsonObject_UpdateString(request,'threadId',threadId);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkJsonObject__lastErrorText(request));
    Exit;
  end;
success := CkHttp_HttpJson(http,'POST','https://gmail.googleapis.com/gmail/v1/users/me/messages/send',request,'application/json',resp);
if (CkHttpResponse_getStatusCode(resp) = 0) then
  begin
    Memo1.Lines.Add('Gmail did not return an HTTP response.');
    Exit;
  end;
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) < 200) or (CkHttpResponse_getStatusCode(resp) >= 300)) then
  begin
    Memo1.Lines.Add('Gmail HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
    success := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
    if (success = True) then
      begin
        Memo1.Lines.Add(CkJsonObject__stringOf(reply,'error.message'));
      end;
    Exit;
  end;
success := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
if (success <> True) then
  begin
    Memo1.Lines.Add('Expected a Gmail JSON response.');
    Exit;
  end;
if ((CkJsonObject_JsonTypeOf(reply,'id') <> 1) or (CkJsonObject_StringOfEquals(reply,'id','',True) = True)) then
  begin
    Memo1.Lines.Add('Expected id in the response.');
    Exit;
  end;
Memo1.Lines.Add('Sent reply ID: ' + CkJsonObject__stringOf(reply,'id'));
Memo1.Lines.Add('Thread ID: ' + CkJsonObject__stringOf(reply,'threadId'));

CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);
CkStringBuilder_Dispose(url);
CkBinData_Dispose(originalMime);
CkEmail_Dispose(original);
CkEmail_Dispose(email);
CkStringBuilder_Dispose(references);
CkBinData_Dispose(mime);
CkJsonObject_Dispose(request);