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.
Chilkat Lazarus Pascal Downloads
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.StringBuilder,
Chilkat.Imap,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
token: TJsonObject;
mailbox: string;
imap: TImap;
parent: string;
folders: TMailboxes;
separator: string;
child: TStringBuilder;
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;
// This creates a custom Gmail label and one nested label.
parent := 'Example';
folders := TMailboxes.Create;
success := imap.MbxList(False,'','*',folders);
if (success <> True) then
begin
imap.Logout();
imap.Disconnect();
WriteLn('Could not list existing labels.');
Exit;
end;
separator := imap.SeparatorChar;
if (separator = '') then
begin
imap.Logout();
imap.Disconnect();
WriteLn('The server did not provide a hierarchy separator.');
Exit;
end;
if (folders.GetMailboxIndex(parent) < 0) then
begin
success := imap.CreateMailbox(parent);
if (success <> True) then
begin
imap.Logout();
imap.Disconnect();
WriteLn('Could not create the parent label.');
Exit;
end;
end;
child := TStringBuilder.Create;
child.Append(parent);
child.Append(separator);
child.Append('Child');
if (folders.GetMailboxIndex(child.GetAsString()) < 0) then
begin
success := imap.CreateMailbox(child.GetAsString());
if (success <> True) then
begin
imap.Logout();
imap.Disconnect();
WriteLn('Could not create the child label. The parent may already exist.');
Exit;
end;
end;
WriteLn('Labels available: ' + parent + ' and ' + child.GetAsString());
imap.Logout();
imap.Disconnect();
token.Free;
imap.Free;
folders.Free;
child.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.