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

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.

local token = chilkat.JsonObject.new()
local success = token:load_file("gmail-mail-token.json")
if not success then
    print("Could not read gmail-mail-token.json from the private working directory.")
    return
end

if (token:json_type_of("access_token") ~= 1) or (token:string_of_equals("access_token", "", true)) then
    print("An OAuth access token is required. Authorize or refresh the account first.")
    return
end

local mailbox = "user@example.com"
local imap = chilkat.Imap.new()
imap.ssl = true
imap.port = 993
imap.require_ssl_cert_verify = true
imap.auth_method = "XOAUTH2"
success = imap:connect("imap.gmail.com")
if not success then
    print("Could not establish the Gmail IMAP TLS connection.")
    return
end

success = imap:login(mailbox, token:string_of("access_token"))
if not success then
    imap:disconnect()
    print("IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy.")
    return
end

-- This creates a custom Gmail label and one nested label.
local parent = "Example"
local folders = chilkat.Mailboxes.new()
success = imap:mbx_list(false, "", "*", folders)
if not success then
    imap:logout()
    imap:disconnect()
    print("Could not list existing labels.")
    return
end

local separator = imap.separator_char
if separator == "" then
    imap:logout()
    imap:disconnect()
    print("The server did not provide a hierarchy separator.")
    return
end

if folders:get_mailbox_index(parent) < 0 then
    success = imap:create_mailbox(parent)
    if not success then
        imap:logout()
        imap:disconnect()
        print("Could not create the parent label.")
        return
    end
end

local child = chilkat.StringBuilder.new()
child:append(parent)
child:append(separator)
child:append("Child")
if folders:get_mailbox_index(child:get_as_string()) < 0 then
    success = imap:create_mailbox(child:get_as_string())
    if not success then
        imap:logout()
        imap:disconnect()
        print("Could not create the child label. The parent may already exist.")
        return
    end
end

print("Labels available: " .. parent .. " and " .. child:get_as_string())
imap:logout()
imap:disconnect()