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

Fetch Oldest/Newest IMAP Email

Emails may be downloaded by sequence number. Assuming the selected mailbox is not empty, the oldest email is at sequence number 1, and the newest email is at sequence number N. The FetchSingle method may be used to download by sequence number.

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.Email;

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

procedure RunDemo;
var
  success: Boolean;
  imap: TImap;
  n: Integer;
  isUid: Boolean;
  oldestEmail: TEmail;
  newestEmail: TEmail;

begin
  success := False;

  //  This example assumes the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  imap := TImap.Create;

  //  Connect to an IMAP server.
  //  Use TLS
  imap.Ssl := True;
  imap.Port := 993;
  success := imap.Connect('mail.testemail.net');
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  //  Login
  success := imap.Login('***','***');
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  //  Select an IMAP mailbox
  success := imap.SelectMailbox('Inbox');
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  //  After selecting a mailbox, the NumMessages property
  //  contains the number of emails in the selected mailbox.

  n := imap.NumMessages;

  if (n > 0) then
    begin

      //  The oldest email is always at sequence number 1.
      isUid := False;

      oldestEmail := TEmail.Create;
      success := imap.FetchEmail(False,1,isUid,oldestEmail);
      if (success = False) then
        begin
          WriteLn(imap.LastErrorText);
          Exit;
        end;

      //  Display the From and Subject
      WriteLn(oldestEmail.FromAddress);
      WriteLn(oldestEmail.Subject);

      WriteLn('--');

      //  The newest email is at sequence number N:
      newestEmail := TEmail.Create;
      success := imap.FetchEmail(False,n,isUid,newestEmail);
      if (success = False) then
        begin
          WriteLn(imap.LastErrorText);
          Exit;
        end;

      //  Display the From and Subject
      WriteLn(newestEmail.FromAddress);
      WriteLn(newestEmail.Subject);

    end;

  //  Disconnect from the IMAP server.
  success := imap.Disconnect();


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