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

Set a Single Flag on One IMAP Message

See more IMAP Examples

Demonstrates the Chilkat Imap.SetFlag method, which sets or clears a single flag on one message identified by ID. The first argument is the message ID, the second (bUid) selects UID vs sequence number, the third is the flag name, and the fourth is the value (1 to set, 0 to clear). This example flags one message as Deleted and then calls Expunge to permanently remove it.

Background: Deleting an IMAP message is a two-step process by design: setting the Deleted flag only marks the message, and Expunge is what actually removes every message so marked. This separation lets a client show "deleted" items and offer an undo before the change becomes permanent. SetFlag handles one flag on one message; use StoreFlags to change several flags at once, or SetFlags to update a whole MessageSet.

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

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

procedure RunDemo;
var
  success: Boolean;
  imap: TImap;
  msgSet: TMessageSet;
  uid: LongWord;

begin
  success := False;

  //  Demonstrates the Imap.SetFlag method, which sets or clears a single flag on one message
  //  identified by ID.  The 1st argument is the message ID, the 2nd (bUid) selects UID vs
  //  sequence number, the 3rd is the flag name, and the 4th is the value (1 to set, 0 to clear).

  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;

  success := imap.SelectMailbox('Inbox');
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  msgSet := TMessageSet.Create;
  success := imap.QueryMbx('ALL',True,msgSet);
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  if (msgSet.Count > 0) then
    begin
      //  Mark the first message for deletion.  The "Deleted" flag is later acted upon by Expunge.
      uid := msgSet.GetId(0);
      success := imap.SetFlag(uid,True,'Deleted',1);
      if (success = False) then
        begin
          WriteLn(imap.LastErrorText);
          Exit;
        end;

      //  Permanently remove all messages flagged as Deleted.
      success := imap.Expunge();
      if (success = False) then
        begin
          WriteLn(imap.LastErrorText);
          Exit;
        end;
      WriteLn('Deleted UID ' + uid);
    end;

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


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