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

Connect to an IMAP Server

See more IMAP Examples

Demonstrates the Chilkat Imap.Connect method, which establishes a TCP connection to the IMAP server but does not authenticate. The argument may be a hostname, IPv4, or IPv6 address; configure Ssl and Port first. This example connects with implicit TLS, then logs in and disconnects.

Background: IMAP (Internet Message Access Protocol) is the protocol for accessing mail that stays on the server — unlike POP3, which typically downloads and removes it. That server-side model is what lets multiple devices see the same mailboxes, folders, and read/unread state. A session starts by connecting (usually implicit TLS on port 993), then authenticating — Connect performs only the first step, so it is always followed by Login.

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;

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

procedure RunDemo;
var
  success: Boolean;
  imap: TImap;

begin
  success := False;

  //  Demonstrates the Imap.Connect method, which establishes a TCP connection to the IMAP
  //  server but does not authenticate.  Call Login after connecting to authenticate.

  imap := TImap.Create;

  //  Use implicit TLS on the standard IMAPS port.
  imap.Ssl := True;
  imap.Port := 993;

  //  Establish the connection to the IMAP server (no authentication yet).
  success := imap.Connect('imap.example.com');
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;
  WriteLn('Connected to the IMAP server.');

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

  //  Disconnect when finished.
  success := imap.Disconnect();
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;


  imap.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.