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

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkMailboxes.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkImap.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.

    token.i = CkJsonObject::ckCreate()
    If token.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckLoadFile(token,"gmail-mail-token.json")
    If success <> 1
        Debug "Could not read gmail-mail-token.json from the private working directory."
        CkJsonObject::ckDispose(token)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(token,"access_token") <> 1) OR (CkJsonObject::ckStringOfEquals(token,"access_token","",1) = 1)
        Debug "An OAuth access token is required. Authorize or refresh the account first."
        CkJsonObject::ckDispose(token)
        ProcedureReturn
    EndIf

    mailbox.s = "user@example.com"
    imap.i = CkImap::ckCreate()
    If imap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)
    CkImap::setCkRequireSslCertVerify(imap, 1)
    CkImap::setCkAuthMethod(imap, "XOAUTH2")
    success = CkImap::ckConnect(imap,"imap.gmail.com")
    If success <> 1
        Debug "Could not establish the Gmail IMAP TLS connection."
        CkJsonObject::ckDispose(token)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckLogin(imap,mailbox,CkJsonObject::ckStringOf(token,"access_token"))
    If success <> 1
        CkImap::ckDisconnect(imap)
        Debug "IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy."
        CkJsonObject::ckDispose(token)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ;  This creates a custom Gmail label and one nested label.
    parent.s = "Example"
    folders.i = CkMailboxes::ckCreate()
    If folders.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckMbxList(imap,0,"","*",folders)
    If success <> 1
        CkImap::ckLogout(imap)
        CkImap::ckDisconnect(imap)
        Debug "Could not list existing labels."
        CkJsonObject::ckDispose(token)
        CkImap::ckDispose(imap)
        CkMailboxes::ckDispose(folders)
        ProcedureReturn
    EndIf

    separator.s = CkImap::ckSeparatorChar(imap)
    If separator = ""
        CkImap::ckLogout(imap)
        CkImap::ckDisconnect(imap)
        Debug "The server did not provide a hierarchy separator."
        CkJsonObject::ckDispose(token)
        CkImap::ckDispose(imap)
        CkMailboxes::ckDispose(folders)
        ProcedureReturn
    EndIf

    If CkMailboxes::ckGetMailboxIndex(folders,parent) < 0
        success = CkImap::ckCreateMailbox(imap,parent)
        If success <> 1
            CkImap::ckLogout(imap)
            CkImap::ckDisconnect(imap)
            Debug "Could not create the parent label."
            CkJsonObject::ckDispose(token)
            CkImap::ckDispose(imap)
            CkMailboxes::ckDispose(folders)
            ProcedureReturn
        EndIf

    EndIf

    child.i = CkStringBuilder::ckCreate()
    If child.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(child,parent)
    CkStringBuilder::ckAppend(child,separator)
    CkStringBuilder::ckAppend(child,"Child")
    If CkMailboxes::ckGetMailboxIndex(folders,CkStringBuilder::ckGetAsString(child)) < 0
        success = CkImap::ckCreateMailbox(imap,CkStringBuilder::ckGetAsString(child))
        If success <> 1
            CkImap::ckLogout(imap)
            CkImap::ckDisconnect(imap)
            Debug "Could not create the child label. The parent may already exist."
            CkJsonObject::ckDispose(token)
            CkImap::ckDispose(imap)
            CkMailboxes::ckDispose(folders)
            CkStringBuilder::ckDispose(child)
            ProcedureReturn
        EndIf

    EndIf

    Debug "Labels available: " + parent + " and " + CkStringBuilder::ckGetAsString(child)
    CkImap::ckLogout(imap)
    CkImap::ckDisconnect(imap)


    CkJsonObject::ckDispose(token)
    CkImap::ckDispose(imap)
    CkMailboxes::ckDispose(folders)
    CkStringBuilder::ckDispose(child)


    ProcedureReturn
EndProcedure