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

Set or Clear a Flag on a MessageSet

See more IMAP Examples

Demonstrates the Chilkat Imap.SetFlags method, which sets or clears a flag on every message in a MessageSet. The first argument is the MessageSet, the second is the flag name, and the third is the value (1 to set the flag, 0 to clear it). This example marks all unseen messages as Seen.

Background: IMAP flags are per-message keywords the server stores and shares across all clients. The standard system flags are Seen, Answered, Flagged, Deleted, Draft, and Recent. Give the flag name without the leading backslash here (use "Seen", not "\Seen"). SetFlags applies the change to the whole set in one round trip, which is how a client implements "mark all as read."

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;

begin
  success := False;

  //  Demonstrates the Imap.SetFlags method, which sets or clears a flag on every message in a
  //  MessageSet.  The 1st argument is the MessageSet, the 2nd is the flag name, and the 3rd is
  //  the value (1 to set the flag, 0 to clear it).

  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;

  //  Find the messages to flag (here, all unseen messages, by UID).
  msgSet := TMessageSet.Create;
  success := imap.QueryMbx('UNSEEN',True,msgSet);
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  //  Mark all of them as seen.  A value of 1 sets the flag; 0 would clear it.
  success := imap.SetFlags(msgSet,'Seen',1);
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  WriteLn('Marked ' + msgSet.Count + ' messages as Seen.');

  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.