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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Token
string ls_Mailbox
oleobject loo_Imap
string ls_Parent
oleobject loo_Folders
string ls_Separator
oleobject loo_Child
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
destroy loo_Token
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_Token.LoadFile("gmail-mail-token.json")
if li_Success <> 1 then
Write-Debug "Could not read gmail-mail-token.json from the private working directory."
destroy loo_Token
return
end if
if (loo_Token.JsonTypeOf("access_token") <> 1) OR (loo_Token.StringOfEquals("access_token","",1) = 1) then
Write-Debug "An OAuth access token is required. Authorize or refresh the account first."
destroy loo_Token
return
end if
ls_Mailbox = "user@example.com"
loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")
loo_Imap.Ssl = 1
loo_Imap.Port = 993
loo_Imap.RequireSslCertVerify = 1
loo_Imap.AuthMethod = "XOAUTH2"
li_Success = loo_Imap.Connect("imap.gmail.com")
if li_Success <> 1 then
Write-Debug "Could not establish the Gmail IMAP TLS connection."
destroy loo_Token
destroy loo_Imap
return
end if
li_Success = loo_Imap.Login(ls_Mailbox,loo_Token.StringOf("access_token"))
if li_Success <> 1 then
loo_Imap.Disconnect()
Write-Debug "IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy."
destroy loo_Token
destroy loo_Imap
return
end if
// This creates a custom Gmail label and one nested label.
ls_Parent = "Example"
loo_Folders = create oleobject
li_rc = loo_Folders.ConnectToNewObject("Chilkat.Mailboxes")
li_Success = loo_Imap.MbxList(0,"","*",loo_Folders)
if li_Success <> 1 then
loo_Imap.Logout()
loo_Imap.Disconnect()
Write-Debug "Could not list existing labels."
destroy loo_Token
destroy loo_Imap
destroy loo_Folders
return
end if
ls_Separator = loo_Imap.SeparatorChar
if ls_Separator = "" then
loo_Imap.Logout()
loo_Imap.Disconnect()
Write-Debug "The server did not provide a hierarchy separator."
destroy loo_Token
destroy loo_Imap
destroy loo_Folders
return
end if
if loo_Folders.GetMailboxIndex(ls_Parent) < 0 then
li_Success = loo_Imap.CreateMailbox(ls_Parent)
if li_Success <> 1 then
loo_Imap.Logout()
loo_Imap.Disconnect()
Write-Debug "Could not create the parent label."
destroy loo_Token
destroy loo_Imap
destroy loo_Folders
return
end if
end if
loo_Child = create oleobject
li_rc = loo_Child.ConnectToNewObject("Chilkat.StringBuilder")
loo_Child.Append(ls_Parent)
loo_Child.Append(ls_Separator)
loo_Child.Append("Child")
if loo_Folders.GetMailboxIndex(loo_Child.GetAsString()) < 0 then
li_Success = loo_Imap.CreateMailbox(loo_Child.GetAsString())
if li_Success <> 1 then
loo_Imap.Logout()
loo_Imap.Disconnect()
Write-Debug "Could not create the child label. The parent may already exist."
destroy loo_Token
destroy loo_Imap
destroy loo_Folders
destroy loo_Child
return
end if
end if
Write-Debug "Labels available: " + ls_Parent + " and " + loo_Child.GetAsString()
loo_Imap.Logout()
loo_Imap.Disconnect()
destroy loo_Token
destroy loo_Imap
destroy loo_Folders
destroy loo_Child