Sample code for 30+ languages & platforms
AutoIt 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 AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.

$oToken = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oToken.LoadFile("gmail-mail-token.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not read gmail-mail-token.json from the private working directory." & @CRLF)
    Exit
EndIf

If (($oToken.JsonTypeOf("access_token") <> 1) Or ($oToken.StringOfEquals("access_token","",True) = True)) Then
    ConsoleWrite("An OAuth access token is required. Authorize or refresh the account first." & @CRLF)
    Exit
EndIf

Local $sMailbox = "user@example.com"
$oImap = ObjCreate("Chilkat.Imap")
$oImap.Ssl = True
$oImap.Port = 993
$oImap.RequireSslCertVerify = True
$oImap.AuthMethod = "XOAUTH2"
$bSuccess = $oImap.Connect("imap.gmail.com")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not establish the Gmail IMAP TLS connection." & @CRLF)
    Exit
EndIf

$bSuccess = $oImap.Login($sMailbox,$oToken.StringOf("access_token"))
If ($bSuccess <> True) Then
    $oImap.Disconnect()
    ConsoleWrite("IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy." & @CRLF)
    Exit
EndIf

;  This creates a custom Gmail label and one nested label.
Local $sParent = "Example"
$oFolders = ObjCreate("Chilkat.Mailboxes")
$bSuccess = $oImap.MbxList(False,"","*",$oFolders)
If ($bSuccess <> True) Then
    $oImap.Logout()
    $oImap.Disconnect()
    ConsoleWrite("Could not list existing labels." & @CRLF)
    Exit
EndIf

Local $separator = $oImap.SeparatorChar
If ($separator = "") Then
    $oImap.Logout()
    $oImap.Disconnect()
    ConsoleWrite("The server did not provide a hierarchy separator." & @CRLF)
    Exit
EndIf

If ($oFolders.GetMailboxIndex($sParent) < 0) Then
    $bSuccess = $oImap.CreateMailbox($sParent)
    If ($bSuccess <> True) Then
        $oImap.Logout()
        $oImap.Disconnect()
        ConsoleWrite("Could not create the parent label." & @CRLF)
        Exit
    EndIf

EndIf

$oChild = ObjCreate("Chilkat.StringBuilder")
$oChild.Append($sParent)
$oChild.Append($separator)
$oChild.Append("Child")
If ($oFolders.GetMailboxIndex($oChild.GetAsString()) < 0) Then
    $bSuccess = $oImap.CreateMailbox($oChild.GetAsString())
    If ($bSuccess <> True) Then
        $oImap.Logout()
        $oImap.Disconnect()
        ConsoleWrite("Could not create the child label. The parent may already exist." & @CRLF)
        Exit
    EndIf

EndIf

ConsoleWrite("Labels available: " & $sParent & " and " & $oChild.GetAsString() & @CRLF)
$oImap.Logout()
$oImap.Disconnect()