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

Delete a POP3 Message by Message Number

See more POP3 Examples

Demonstrates the Chilkat MailMan.DeleteByMsgnum method, which marks an email for deletion by its POP3 message number. Message numbers are specific to a single POP3 session and can change from one session to the next, so UIDLs are generally preferred for stable identification. This example marks message number 1 for deletion.

Background: In POP3, deletion is two-phase: messages are marked with the DELE command during the session, and the server only actually removes them when the session ends cleanly with QUIT. Message numbers are simple sequence positions (1, 2, 3, …) valid only within the current session — which is why DeleteByUidl is safer when you need to reliably target a specific message.

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

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

procedure RunDemo;
var
  success: Boolean;
  mailman: TMailMan;

begin
  success := False;

  //  Demonstrates the MailMan.DeleteByMsgnum method, which marks an email for deletion by its
  //  POP3 message number.  Message numbers are specific to a single POP3 session and can
  //  change between sessions, so UIDLs are generally preferred.

  mailman := TMailMan.Create;

  //  Configure the POP3 server connection.
  mailman.MailHost := 'pop.example.com';
  mailman.MailPort := 995;
  mailman.PopSsl := True;
  mailman.PopUsername := 'user@example.com';
  mailman.PopPassword := 'myPassword';

  //  Mark message number 1 for deletion.

  success := mailman.DeleteByMsgnum(1);
  if (success = False) then
    begin
      WriteLn(mailman.LastErrorText);
      Exit;
    end;

  //  Unless the ImmediateDelete property is set to True, the message is only marked for
  //  deletion.  End the POP3 session (which sends the QUIT command) to commit the deletion
  //  on the server.
  success := mailman.Pop3EndSession();
  if (success = False) then
    begin
      WriteLn(mailman.LastErrorText);
      Exit;
    end;

  WriteLn('Deleted message 1 from the POP3 server.');

  //  Note: Explicitly connecting/authenticating is optional.  Chilkat MailMan automatically
  //  connects and authenticates -- using the property settings above -- whenever a server
  //  operation requires it.  Calling the explicit connect/authenticate methods can still be
  //  helpful to determine whether a failure occurs while connecting or while authenticating.


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