Sample code for 30+ languages & platforms
Delphi ActiveX 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 ActiveX Downloads

Delphi ActiveX
var
success: Integer;
token: TChilkatJsonObject;
http: TChilkatHttp;
resp: TChilkatHttpResponse;
reply: TChilkatJsonObject;
url: TChilkatStringBuilder;
threadId: WideString;
originalMime: TChilkatBinData;
original: TChilkatEmail;
messageIdHeader: WideString;
email: TChilkatEmail;
replyAddress: WideString;
references: TChilkatStringBuilder;
previousReferences: WideString;
mime: TChilkatBinData;
request: TChilkatJsonObject;

begin
success := 0;

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

token := TChilkatJsonObject.Create(Self);
success := token.LoadFile('gmail-token.json');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not read gmail-token.json from the private working directory.');
    Exit;
  end;
if ((token.JsonTypeOf('access_token') <> 1) or (token.StringOfEquals('access_token','',1) = 1)) then
  begin
    Memo1.Lines.Add('An OAuth access token is required.');
    Exit;
  end;
http := TChilkatHttp.Create(Self);
http.RequireSslCertVerify := 1;
http.AuthToken := token.StringOf('access_token');
resp := TChilkatHttpResponse.Create(Self);
reply := TChilkatJsonObject.Create(Self);
url := TChilkatStringBuilder.Create(Self);
url.Append('https://gmail.googleapis.com/gmail/v1/users/me/messages/');
url.Append('GMAIL_MESSAGE_ID');
url.Append('?format=raw');
success := http.HttpNoBody('GET',url.GetAsString(),resp.ControlInterface);
if (resp.StatusCode = 0) then
  begin
    Memo1.Lines.Add('Gmail did not return an HTTP response.');
    Exit;
  end;
if ((success <> 1) or (resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
  begin
    Memo1.Lines.Add('Gmail HTTP ' + IntToStr(resp.StatusCode));
    success := reply.Load(resp.BodyStr);
    if (success = 1) then
      begin
        Memo1.Lines.Add(reply.StringOf('error.message'));
      end;
    Exit;
  end;
success := reply.Load(resp.BodyStr);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Expected a Gmail JSON response.');
    Exit;
  end;
if ((reply.JsonTypeOf('threadId') <> 1) or (reply.StringOfEquals('threadId','',1) = 1)) then
  begin
    Memo1.Lines.Add('Expected threadId in the response.');
    Exit;
  end;
if ((reply.JsonTypeOf('raw') <> 1) or (reply.StringOfEquals('raw','',1) = 1)) then
  begin
    Memo1.Lines.Add('Expected raw in the response.');
    Exit;
  end;
threadId := reply.StringOf('threadId');
originalMime := TChilkatBinData.Create(Self);
success := reply.BytesOf('raw','base64url',originalMime.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add(reply.LastErrorText);
    Exit;
  end;
original := TChilkatEmail.Create(Self);
success := original.SetFromMimeBd(originalMime.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add(original.LastErrorText);
    Exit;
  end;
messageIdHeader := original.GetHeaderField('Message-ID');
if ((original.LastMethodSuccess <> 1) or (messageIdHeader = '')) then
  begin
    Memo1.Lines.Add('The original message needs a Message-ID for this threaded reply.');
    Exit;
  end;
email := TChilkatEmail.Create(Self);
email.From := 'Sender <sender@example.com>';
//  Use the parsed Reply-To mailbox list when present, otherwise use From.
replyAddress := original.ReplyTo;
if (replyAddress = '') then
  begin
    replyAddress := original.From;
  end;
success := email.AddMultipleTo(replyAddress);
if (success <> 1) then
  begin
    Memo1.Lines.Add(email.LastErrorText);
    Exit;
  end;
if (email.NumTo = 0) then
  begin
    Memo1.Lines.Add('The original message has no usable reply address.');
    Exit;
  end;
email.Subject := original.Subject;
email.Charset := 'utf-8';
email.Body := 'Thank you for your message.';
email.AddHeaderField('In-Reply-To',messageIdHeader);
references := TChilkatStringBuilder.Create(Self);
previousReferences := original.GetHeaderField('References');
if ((original.LastMethodSuccess = 1) and (previousReferences <> '')) then
  begin
    references.Append(previousReferences);
    references.Append(' ');
  end;
references.Append(messageIdHeader);
email.AddHeaderField('References',references.GetAsString());
mime := TChilkatBinData.Create(Self);
success := email.GetMimeBd(mime.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add(email.LastErrorText);
    Exit;
  end;
request := TChilkatJsonObject.Create(Self);
success := request.UpdateBd('raw','base64url',mime.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add(request.LastErrorText);
    Exit;
  end;
success := request.UpdateString('threadId',threadId);
if (success <> 1) then
  begin
    Memo1.Lines.Add(request.LastErrorText);
    Exit;
  end;
success := http.HttpJson('POST','https://gmail.googleapis.com/gmail/v1/users/me/messages/send',request.ControlInterface,'application/json',resp.ControlInterface);
if (resp.StatusCode = 0) then
  begin
    Memo1.Lines.Add('Gmail did not return an HTTP response.');
    Exit;
  end;
if ((success <> 1) or (resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
  begin
    Memo1.Lines.Add('Gmail HTTP ' + IntToStr(resp.StatusCode));
    success := reply.Load(resp.BodyStr);
    if (success = 1) then
      begin
        Memo1.Lines.Add(reply.StringOf('error.message'));
      end;
    Exit;
  end;
success := reply.Load(resp.BodyStr);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Expected a Gmail JSON response.');
    Exit;
  end;
if ((reply.JsonTypeOf('id') <> 1) or (reply.StringOfEquals('id','',1) = 1)) then
  begin
    Memo1.Lines.Add('Expected id in the response.');
    Exit;
  end;
Memo1.Lines.Add('Sent reply ID: ' + reply.StringOf('id'));
Memo1.Lines.Add('Thread ID: ' + reply.StringOf('threadId'));