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

Store One or More Flags on a Single Message

See more IMAP Examples

Demonstrates the Chilkat Imap.StoreFlags method, which sets or clears one or more flags on a single message identified by ID. The first argument is the message ID, the second (bUid) selects UID vs sequence number, the third is a space-separated list of flag names, and the fourth is the value (1 to set, 0 to clear). This example applies the Seen and Flagged flags to one message by UID.

Background: StoreFlags is the single-message counterpart to SetFlags and lets you change several flags at once by listing them space-separated (without leading backslashes). Because the ID can be either a UID or a sequence number, the bUid argument must match how you obtained it — a UID from a UID search, or a sequence number from a sequence-based operation. UIDs are the safer choice since they remain valid as the mailbox changes.

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.StoreFlags method, which sets or clears one or more flags on a single
  //  message identified by ID.  The 1st argument is the message ID, the 2nd (bUid) selects UID
  //  vs sequence number, the 3rd is a space-separated list of flag names, 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;

  //  Get the UIDs of all messages.
  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
      //  Apply the Seen and Flagged flags to the first message.  bUid is True because the ID
      //  came from a UID search.
      uid := msgSet.GetId(0);
      success := imap.StoreFlags(uid,True,'Seen Flagged',1);
      if (success = False) then
        begin
          WriteLn(imap.LastErrorText);
          Exit;
        end;
      WriteLn('Updated flags on 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.