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 VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 0
' Assumes Chilkat is unlocked. See Global Unlock Sample.
set token = CreateObject("Chilkat.JsonObject")
success = token.LoadFile("gmail-mail-token.json")
If (success <> 1) Then
outFile.WriteLine("Could not read gmail-mail-token.json from the private working directory.")
WScript.Quit
End If
If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",1) = 1)) Then
outFile.WriteLine("An OAuth access token is required. Authorize or refresh the account first.")
WScript.Quit
End If
mailbox = "user@example.com"
set imap = CreateObject("Chilkat.Imap")
imap.Ssl = 1
imap.Port = 993
imap.RequireSslCertVerify = 1
imap.AuthMethod = "XOAUTH2"
success = imap.Connect("imap.gmail.com")
If (success <> 1) Then
outFile.WriteLine("Could not establish the Gmail IMAP TLS connection.")
WScript.Quit
End If
success = imap.Login(mailbox,token.StringOf("access_token"))
If (success <> 1) Then
success = imap.Disconnect()
outFile.WriteLine("IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy.")
WScript.Quit
End If
' This creates a custom Gmail label and one nested label.
parent = "Example"
set folders = CreateObject("Chilkat.Mailboxes")
success = imap.MbxList(0,"","*",folders)
If (success <> 1) Then
success = imap.Logout()
success = imap.Disconnect()
outFile.WriteLine("Could not list existing labels.")
WScript.Quit
End If
separator = imap.SeparatorChar
If (separator = "") Then
success = imap.Logout()
success = imap.Disconnect()
outFile.WriteLine("The server did not provide a hierarchy separator.")
WScript.Quit
End If
If (folders.GetMailboxIndex(parent) < 0) Then
success = imap.CreateMailbox(parent)
If (success <> 1) Then
success = imap.Logout()
success = imap.Disconnect()
outFile.WriteLine("Could not create the parent label.")
WScript.Quit
End If
End If
set child = CreateObject("Chilkat.StringBuilder")
success = child.Append(parent)
success = child.Append(separator)
success = child.Append("Child")
If (folders.GetMailboxIndex(child.GetAsString()) < 0) Then
success = imap.CreateMailbox(child.GetAsString())
If (success <> 1) Then
success = imap.Logout()
success = imap.Disconnect()
outFile.WriteLine("Could not create the child label. The parent may already exist.")
WScript.Quit
End If
End If
outFile.WriteLine("Labels available: " & parent & " and " & child.GetAsString())
success = imap.Logout()
success = imap.Disconnect()
outFile.Close