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

Find an email by subject and fetch it by IMAP UID

See more Email Object Examples

Search the selected INBOX by subject and fetch the first matching UID into an Email. The example requests complete attachment bodies and uses peek mode to preserve the read flag. Configure your own IMAP TLS endpoint and credentials.

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.Imap,
  Chilkat.Email,
  Chilkat.MessageSet;

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

procedure RunDemo;
var
  success: Boolean;
  imap: TImap;
  matches: TMessageSet;
  uid: LongWord;
  email: TEmail;

begin
  success := False;

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

  imap := TImap.Create;
  imap.Ssl := True;
  imap.Port := 993;
  imap.RequireSslCertVerify := True;
  imap.PeekMode := True;
  imap.AutoDownloadAttachments := True;
  success := imap.Connect('imap.example.com');
  if (success <> True) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;
  success := imap.Login('MAIL_USER','MAIL_PASSWORD');
  if (success <> True) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;
  success := imap.SelectMailbox('INBOX');
  if (success <> True) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;
  matches := TMessageSet.Create;
  success := imap.QueryMbx('SUBJECT "Sample message"',True,matches);
  if (success <> True) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;
  if (matches.Count = 0) then
    begin
      WriteLn('No matching message in INBOX.');
      success := imap.Disconnect();
      if (success <> True) then
        begin
          WriteLn(imap.LastErrorText);
          Exit;
        end;
      Exit;
    end;
  //  The search returned UIDs. Use the same identifier type for the fetch.
  uid := matches.GetId(0);
  email := TEmail.Create;
  success := imap.FetchEmail(False,uid,True,email);
  if (success <> True) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;
  WriteLn('Mailbox INBOX, UIDVALIDITY ' + imap.UidValidity + ', UID ' + uid);
  success := imap.Disconnect();
  if (success <> True) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;
  //  The downloaded Email remains usable after disconnecting.
  WriteLn('Subject: ' + email.Subject + '  Files: ' + email.NumAttachments);

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


  imap.Free;
  matches.Free;
  email.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.