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

Create an IMAP Folder: Gmail Labels with OAuth2

Create a custom Gmail label and a nested label using CreateMailbox. The example lists existing mailboxes first, skips names already present, and uses the hierarchy separator returned by LIST.

This creates labels in the account. A top-level name is not automatically a child of INBOX. In Gmail, IMAP mailboxes represent labels; creating a label does not apply it to a message. Chilkat handles Unicode mailbox names in its mailbox methods. If creating the child fails, the parent remains. A concurrent creation can require listing again.

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 Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
token: TChilkatJsonObject;
mailbox: WideString;
imap: TChilkatImap;
parent: WideString;
folders: TMailboxes;
separator: WideString;
child: TChilkatStringBuilder;

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;
//  This creates a custom Gmail label and one nested label.
parent := 'Example';
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 existing labels.');
    Exit;
  end;
separator := imap.SeparatorChar;
if (separator = '') then
  begin
    imap.Logout();
    imap.Disconnect();
    Memo1.Lines.Add('The server did not provide a hierarchy separator.');
    Exit;
  end;
if (folders.GetMailboxIndex(parent) < 0) then
  begin
    success := imap.CreateMailbox(parent);
    if (success <> 1) then
      begin
        imap.Logout();
        imap.Disconnect();
        Memo1.Lines.Add('Could not create the parent label.');
        Exit;
      end;
  end;
child := TChilkatStringBuilder.Create(Self);
child.Append(parent);
child.Append(separator);
child.Append('Child');
if (folders.GetMailboxIndex(child.GetAsString()) < 0) then
  begin
    success := imap.CreateMailbox(child.GetAsString());
    if (success <> 1) then
      begin
        imap.Logout();
        imap.Disconnect();
        Memo1.Lines.Add('Could not create the child label. The parent may already exist.');
        Exit;
      end;
  end;
Memo1.Lines.Add('Labels available: ' + parent + ' and ' + child.GetAsString());
imap.Logout();
imap.Disconnect();