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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loToken
LOCAL lcMailbox
LOCAL loImap
LOCAL lcParent
LOCAL loFolders
LOCAL lcSeparator
LOCAL loChild
lnSuccess = 0
* Assumes Chilkat is unlocked. See Global Unlock Sample.
loToken = CreateObject('Chilkat.JsonObject')
lnSuccess = loToken.LoadFile("gmail-mail-token.json")
IF (lnSuccess <> 1) THEN
? "Could not read gmail-mail-token.json from the private working directory."
RELEASE loToken
CANCEL
ENDIF
IF ((loToken.JsonTypeOf("access_token") <> 1) OR (loToken.StringOfEquals("access_token","",1) = 1)) THEN
? "An OAuth access token is required. Authorize or refresh the account first."
RELEASE loToken
CANCEL
ENDIF
lcMailbox = "user@example.com"
loImap = CreateObject('Chilkat.Imap')
loImap.Ssl = 1
loImap.Port = 993
loImap.RequireSslCertVerify = 1
loImap.AuthMethod = "XOAUTH2"
lnSuccess = loImap.Connect("imap.gmail.com")
IF (lnSuccess <> 1) THEN
? "Could not establish the Gmail IMAP TLS connection."
RELEASE loToken
RELEASE loImap
CANCEL
ENDIF
lnSuccess = loImap.Login(lcMailbox,loToken.StringOf("access_token"))
IF (lnSuccess <> 1) THEN
loImap.Disconnect()
? "IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy."
RELEASE loToken
RELEASE loImap
CANCEL
ENDIF
* This creates a custom Gmail label and one nested label.
lcParent = "Example"
loFolders = CreateObject('Chilkat.Mailboxes')
lnSuccess = loImap.MbxList(0,"","*",loFolders)
IF (lnSuccess <> 1) THEN
loImap.Logout()
loImap.Disconnect()
? "Could not list existing labels."
RELEASE loToken
RELEASE loImap
RELEASE loFolders
CANCEL
ENDIF
lcSeparator = loImap.SeparatorChar
IF (lcSeparator = "") THEN
loImap.Logout()
loImap.Disconnect()
? "The server did not provide a hierarchy separator."
RELEASE loToken
RELEASE loImap
RELEASE loFolders
CANCEL
ENDIF
IF (loFolders.GetMailboxIndex(lcParent) < 0) THEN
lnSuccess = loImap.CreateMailbox(lcParent)
IF (lnSuccess <> 1) THEN
loImap.Logout()
loImap.Disconnect()
? "Could not create the parent label."
RELEASE loToken
RELEASE loImap
RELEASE loFolders
CANCEL
ENDIF
ENDIF
loChild = CreateObject('Chilkat.StringBuilder')
loChild.Append(lcParent)
loChild.Append(lcSeparator)
loChild.Append("Child")
IF (loFolders.GetMailboxIndex(loChild.GetAsString()) < 0) THEN
lnSuccess = loImap.CreateMailbox(loChild.GetAsString())
IF (lnSuccess <> 1) THEN
loImap.Logout()
loImap.Disconnect()
? "Could not create the child label. The parent may already exist."
RELEASE loToken
RELEASE loImap
RELEASE loFolders
RELEASE loChild
CANCEL
ENDIF
ENDIF
? "Labels available: " + lcParent + " and " + loChild.GetAsString()
loImap.Logout()
loImap.Disconnect()
RELEASE loToken
RELEASE loImap
RELEASE loFolders
RELEASE loChild