Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

List IMAP Mailboxes

See more IMAP Examples

Demonstrates the Chilkat Imap.MbxList method, which lists matching mailboxes and appends them to a Mailboxes object. The first argument selects subscribed-only, the second is a reference name, the third is the mailbox name pattern, and the fourth is the Mailboxes object. This example lists all mailboxes and prints each name. (The object is not cleared on success, so avoid reusing one across calls to prevent duplicates.)

Background: This corresponds to the IMAP LIST command (or LSUB when subscribed-only is requested). The pattern uses IMAP wildcards: * matches across hierarchy levels while % matches within a single level, so Archive/% lists just the direct children of Archive. Each returned mailbox also carries flags you can inspect — for example HasInferiors (has sub-folders) and IsSelectable — which is how a client renders a folder tree.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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.Mailboxes;

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

procedure RunDemo;
var
  success: Boolean;
  imap: TImap;
  mailboxes: TMailboxes;
  n: Integer;
  i: Integer;

begin
  success := False;

  //  Demonstrates the Imap.MbxList method, which lists matching mailboxes and appends them to a
  //  Mailboxes object.  The 1st argument selects subscribed-only, the 2nd is a reference name,
  //  the 3rd is the mailbox pattern, and the 4th is the Mailboxes object.

  imap := TImap.Create;

  imap.Ssl := True;
  imap.Port := 993;

  success := imap.Connect('imap.example.com');
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;
  success := imap.Login('user@example.com','myPassword');
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  //  List all mailboxes.  The first argument False requests all (not subscribed-only); "*" matches any name.
  mailboxes := TMailboxes.Create;
  success := imap.MbxList(False,'','*',mailboxes);
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  n := mailboxes.Count;
  WriteLn('Mailboxes: ' + n);

  for i := 0 to n - 1 do
    begin
      WriteLn(mailboxes.GetName(i));
    end;

  success := imap.Disconnect;ERROR: ";" expected



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