Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

Reply to an email and preserve its thread

See more Email Object Examples

Create a reply to with-attachments.eml. Address it to Reply-To when supplied, or to the original author otherwise. Link the reply to the original Message-ID, replace its body with a short answer, and print the destination before saving the draft.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Email,
  Chilkat.StringBuilder;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  original: TEmail;
  reply: TEmail;
  destination: string;
  parentId: string;
  refs: TStringBuilder;

begin
  success := False;

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

  original := TEmail.Create;
  success := original.LoadEml('with-attachments.eml');
  if (success <> True) then
    begin
      WriteLn(original.LastErrorText);
      Exit;
    end;
  reply := TEmail.Create;
  success := original.ToReply(reply);
  if (success <> True) then
    begin
      WriteLn(original.LastErrorText);
      Exit;
    end;
  reply.From := 'Alex Rivera <alex@example.net>';
  //  This is a reply to the author, not a reply-all. Set the destination explicitly.
  reply.ClearTo();
  reply.ClearCC();
  reply.ClearBcc();
  destination := original.GetHeaderField('Reply-To');
  if (destination = '') then
    begin
      destination := original.From;
    end;
  success := reply.AddMultipleTo(destination);
  if (success <> True) then
    begin
      WriteLn(reply.LastErrorText);
      Exit;
    end;
  //  Link this answer to its parent and preserve the earlier conversation references.
  parentId := original.GetHeaderField('Message-ID');
  if (parentId <> '') then
    begin
      reply.AddHeaderField('In-Reply-To',parentId);
      refs := TStringBuilder.Create;
      refs.Append(original.GetHeaderField('References'));
      if (refs.Length > 0) then
        begin
          refs.Append(' ');
        end;
      refs.Append(parentId);
      reply.AddHeaderField('References',refs.GetAsString());
    end;
  //  A short text reply has no use for the original inline artwork or nested correspondence.
  reply.RemoveAttachedMessages();
  reply.RemoveHtmlAlternative();
  reply.DropRelatedItems();
  reply.SetTextBody('Thank you for your message.','text/plain');
  if (reply.NumTo < 1) then
    begin
      WriteLn('Reply has no destination. Review the original sender headers.');
      Exit;
    end;
  WriteLn('Reply goes to: ' + reply.GetToAddr(0));
  WriteLn('In-Reply-To: ' + reply.GetHeaderField('In-Reply-To'));

  success := reply.SaveEml('reply.eml');
  if (success <> True) then
    begin
      WriteLn(reply.LastErrorText);
      Exit;
    end;
  WriteLn('Saved reply.eml.');


  original.Free;
  reply.Free;
      refs.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.