Sample code for 30+ languages & platforms
Delphi ActiveX Requires Chilkat v11.1.0+

Gmail IMAP: Discover Special Folders

See more GMail SMTP/IMAP/POP Examples

Find Gmail special folders from the attributes returned by LIST, using MbxList and Mailboxes.HasFlag. The result maps roles such as Sent and Trash to the server-provided mailbox names. Chilkat decodes mailbox names for use with its mailbox methods.

Folder names can be localized and visibility can differ by account. The role attributes are stable; do not hard-code [Gmail]/Sent Mail or use the deprecated XLIST command. INBOX is a protocol-defined name. Missing map keys indicate folders not advertised by the server.

Use the mailbox that authorized the full-mail scope https://mail.google.com/. Obtain the protocol OAuth token and refresh it before running this example. The token is loaded from gmail-mail-token.json in a private working directory; a narrow Gmail REST token is insufficient.

Google documentation

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
token: TChilkatJsonObject;
mailbox: WideString;
imap: TChilkatImap;
folders: TMailboxes;
special: TChilkatJsonObject;
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 load the protocol token 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('Authorize or refresh the mailbox 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, scope, expiry, and account policy.');
    Exit;
  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 Gmail folders.');
    Exit;
  end;
//  LIST special-use attributes work with localized folder names. XLIST is obsolete.
special := TChilkatJsonObject.Create(Self);
i := 0;
while (i < folders.Count) do
  begin
    if (folders.IsSelectable(i) = 1) then
      begin
        Memo1.Lines.Add(folders.GetName(i) + '  ' + folders.GetFlags(i));
        if (folders.HasFlag(i,'\All') = 1) then
          begin
            special.UpdateString('allMail',folders.GetName(i));
          end;
        if (folders.HasFlag(i,'\Sent') = 1) then
          begin
            special.UpdateString('sent',folders.GetName(i));
          end;
        if (folders.HasFlag(i,'\Drafts') = 1) then
          begin
            special.UpdateString('drafts',folders.GetName(i));
          end;
        if (folders.HasFlag(i,'\Junk') = 1) then
          begin
            special.UpdateString('spam',folders.GetName(i));
          end;
        if (folders.HasFlag(i,'\Trash') = 1) then
          begin
            special.UpdateString('trash',folders.GetName(i));
          end;
        if (folders.HasFlag(i,'\Flagged') = 1) then
          begin
            special.UpdateString('starred',folders.GetName(i));
          end;
        if (folders.HasFlag(i,'\Important') = 1) then
          begin
            special.UpdateString('important',folders.GetName(i));
          end;
      end;
    i := i + 1;
  end;

imap.Logout();
imap.Disconnect();
special.EmitCompact := 0;
Memo1.Lines.Add(special.Emit());
//  Missing keys mean the folder was not advertised. Do not substitute an English name.