Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

Gmail IMAP: Inspect Flags and Locate the Spam Folder

See more IMAP Examples

Inspect the current INBOX flags and discover Gmail's Spam folder through its \Junk special-use attribute. The example reads the first message's flags, if present, and makes no mailbox changes.

Client-specific Junk/NonJunk keywords are not a portable spam verdict. A mailbox's \Junk LIST attribute identifies a special folder; it is not a message flag. Use current server responses rather than a historical transcript. For Gmail label membership, see X-GM-LABELS.

First authorize the mailbox for https://mail.google.com/ and refresh the token as needed. Read the current token from gmail-mail-token.json in a private working directory. Narrow Gmail REST scopes do not authorize these protocols. Keep the token and authentication logs private.

See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.

Reference documentation

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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.Mailboxes,
  Chilkat.Imap,
  Chilkat.JsonObject;

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

procedure RunDemo;
var
  success: Boolean;
  token: TJsonObject;
  mailbox: string;
  imap: TImap;
  sequence: LongWord;
  flags: string;
  folders: TMailboxes;
  i: Integer;

begin
  success := False;

  //  Assumes Chilkat is unlocked. See Global Unlock Sample.

  token := TJsonObject.Create;
  success := token.LoadFile('gmail-mail-token.json');
  if (success <> True) then
    begin
      WriteLn('Could not read gmail-mail-token.json from the private working directory.');
      Exit;
    end;
  if ((token.JsonTypeOf('access_token') <> 1) or (token.StringOfEquals('access_token','',True) = True)) then
    begin
      WriteLn('An OAuth access token is required. Authorize or refresh the account first.');
      Exit;
    end;
  mailbox := 'user@example.com';
  imap := TImap.Create;
  imap.Ssl := True;
  imap.Port := 993;
  imap.RequireSslCertVerify := True;
  imap.AuthMethod := 'XOAUTH2';
  success := imap.Connect('imap.gmail.com');
  if (success <> True) then
    begin
      WriteLn('Could not establish the Gmail IMAP TLS connection.');
      Exit;
    end;
  success := imap.Login(mailbox,token.StringOf('access_token'));
  if (success <> True) then
    begin
      imap.Disconnect();
      WriteLn('IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy.');
      Exit;
    end;
  success := imap.ExamineMailbox('INBOX');
  if (success <> True) then
    begin
      imap.Logout();
      imap.Disconnect();
      WriteLn('Could not examine INBOX.');
      Exit;
    end;
  //  Capture only the EXAMINE response, after authentication has completed.
  WriteLn(imap.LastResponse);
  //  Sample flags without marking messages read. Sequence numbers are session-local.
  if (imap.NumMessages > 0) then
    begin
      sequence := 1;
      flags := imap.FetchFlags(sequence,False);
      if (imap.LastMethodSuccess <> True) then
        begin
          imap.Logout();
          imap.Disconnect();
          WriteLn('Could not fetch flags; the message may have changed or disappeared.');
          Exit;
        end;
      WriteLn('Sequence ' + sequence + ': ' + flags);
    end;
  folders := TMailboxes.Create;
  success := imap.MbxList(False,'','*',folders);
  if (success <> True) then
    begin
      imap.Logout();
      imap.Disconnect();
      WriteLn('Could not list folders.');
      Exit;
    end;
  i := 0;
  while (i < folders.Count) do
    begin
      if (folders.HasFlag(i,'\Junk') = True) then
        begin
          WriteLn('Spam folder: ' + folders.GetName(i));
        end;
      i := i + 1;
    end;

  imap.Logout();
  imap.Disconnect();


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