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

Fetch the Flags Set on an IMAP Message

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchFlags method, which returns the flags currently set on a single message as a space-separated string. The first argument is the message ID and the second (bUid) selects UID vs sequence number. This example fetches and prints the flags for one message identified by UID.

Background: The returned string is the server's current flag list for the message, such as \Seen \Flagged. This is a quick way to check a single message's state — for example, to decide whether it still needs processing — without downloading the message itself. To read the same information from a message you already have as an Email object, use GetMailFlag instead.

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;
  flags: string;

begin
  success := False;

  //  Demonstrates the Imap.FetchFlags method, which returns the flags currently set on a single
  //  message as a space-separated string.  The 1st argument is the message ID and the 2nd (bUid)
  //  selects UID vs sequence number.

  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
      uid := msgSet.GetId(0);
      //  FetchFlags returns a string, so assign it to a string variable and check for success.
      flags := imap.FetchFlags(uid,True);
      if (imap.LastMethodSuccess = False) then
        begin
          WriteLn(imap.LastErrorText);
          Exit;
        end;
      WriteLn('UID ' + uid + ' flags: ' + flags);
    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.