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.
Chilkat Delphi ActiveX Downloads
var
success: Integer;
token: TChilkatJsonObject;
mailbox: WideString;
imap: TChilkatImap;
sequence: Cardinal;
flags: WideString;
folders: TMailboxes;
i: Integer;
begin
success := 0;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
token := TChilkatJsonObject.Create(Self);
success := token.LoadFile('gmail-mail-token.json');
if (success <> 1) then
begin
Memo1.Lines.Add('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','',1) = 1)) then
begin
Memo1.Lines.Add('An OAuth access token is required. Authorize or refresh the account first.');
Exit;
end;
mailbox := 'user@example.com';
imap := TChilkatImap.Create(Self);
imap.Ssl := 1;
imap.Port := 993;
imap.RequireSslCertVerify := 1;
imap.AuthMethod := 'XOAUTH2';
success := imap.Connect('imap.gmail.com');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not establish the Gmail IMAP TLS connection.');
Exit;
end;
success := imap.Login(mailbox,token.StringOf('access_token'));
if (success <> 1) then
begin
imap.Disconnect();
Memo1.Lines.Add('IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy.');
Exit;
end;
success := imap.ExamineMailbox('INBOX');
if (success <> 1) then
begin
imap.Logout();
imap.Disconnect();
Memo1.Lines.Add('Could not examine INBOX.');
Exit;
end;
// Capture only the EXAMINE response, after authentication has completed.
Memo1.Lines.Add(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,0);
if (imap.LastMethodSuccess <> 1) then
begin
imap.Logout();
imap.Disconnect();
Memo1.Lines.Add('Could not fetch flags; the message may have changed or disappeared.');
Exit;
end;
Memo1.Lines.Add('Sequence ' + IntToStr(sequence) + ': ' + flags);
end;
folders := TMailboxes.Create(Self);
success := imap.MbxList(0,'','*',folders.ControlInterface);
if (success <> 1) then
begin
imap.Logout();
imap.Disconnect();
Memo1.Lines.Add('Could not list folders.');
Exit;
end;
i := 0;
while (i < folders.Count) do
begin
if (folders.HasFlag(i,'\Junk') = 1) then
begin
Memo1.Lines.Add('Spam folder: ' + folders.GetName(i));
end;
i := i + 1;
end;
imap.Logout();
imap.Disconnect();