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

Refresh an Email Object's IMAP Flags

See more IMAP Examples

Demonstrates the Chilkat Imap.RefetchMailFlags method, which updates the flags stored on an Email object to the current values on the server. The only argument is the Email object. This example re-reads the flags for a previously fetched message and then checks its Seen state.

Background: Flags are shared mailbox state, so between the time you fetch a message and the time you act on it, another client (a phone, a webmail tab) may have marked it read, flagged, or deleted. RefetchMailFlags pulls just the flags — not the whole message — so a long-running job can cheaply re-check the live state before deciding what to do, avoiding acting on stale information.

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.EmailBundle,
  Chilkat.Imap,
  Chilkat.Email,
  Chilkat.MessageSet;

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

procedure RunDemo;
var
  success: Boolean;
  imap: TImap;
  msgSet: TMessageSet;
  bundle: TEmailBundle;
  email: TEmail;
  seen: Integer;

begin
  success := False;

  //  Demonstrates the Imap.RefetchMailFlags method, which updates the flags stored on an Email
  //  object to the current values on the server.  The only argument is the Email object.

  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;

  bundle := TEmailBundle.Create;
  success := imap.FetchMsgSet(True,msgSet,bundle);
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  email := TEmail.Create;
  if (bundle.MessageCount > 0) then
    begin
      success := bundle.EmailAt(0,email);

      //  Re-read the flags from the server, in case another client changed them since the fetch.
      success := imap.RefetchMailFlags(email);
      if (success = False) then
        begin
          WriteLn(imap.LastErrorText);
          Exit;
        end;

      seen := imap.GetMailFlag(email,'Seen');
      WriteLn('Current Seen state: ' + seen);
    end;

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


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