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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoToken
String sMailbox
Handle hoImap
String sParent
Variant vFolders
Handle hoFolders
String sSeparator
Handle hoChild
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Get Create (RefClass(cComChilkatJsonObject)) To hoToken
If (Not(IsComObjectCreated(hoToken))) Begin
Send CreateComObject of hoToken
End
Get ComLoadFile Of hoToken "gmail-mail-token.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read gmail-mail-token.json from the private working directory."
Procedure_Return
End
Get ComJsonTypeOf Of hoToken "access_token" To iTemp1
Get ComStringOfEquals Of hoToken "access_token" "" True To bTemp1
If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
Showln "An OAuth access token is required. Authorize or refresh the account first."
Procedure_Return
End
Move "user@example.com" To sMailbox
Get Create (RefClass(cComChilkatImap)) To hoImap
If (Not(IsComObjectCreated(hoImap))) Begin
Send CreateComObject of hoImap
End
Set ComSsl Of hoImap To True
Set ComPort Of hoImap To 993
Set ComRequireSslCertVerify Of hoImap To True
Set ComAuthMethod Of hoImap To "XOAUTH2"
Get ComConnect Of hoImap "imap.gmail.com" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not establish the Gmail IMAP TLS connection."
Procedure_Return
End
Get ComStringOf Of hoToken "access_token" To sTemp1
Get ComLogin Of hoImap sMailbox sTemp1 To iSuccess
If (iSuccess <> True) Begin
Get ComDisconnect Of hoImap To iSuccess
Showln "IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy."
Procedure_Return
End
// This creates a custom Gmail label and one nested label.
Move "Example" To sParent
Get Create (RefClass(cComChilkatMailboxes)) To hoFolders
If (Not(IsComObjectCreated(hoFolders))) Begin
Send CreateComObject of hoFolders
End
Get pvComObject of hoFolders to vFolders
Get ComMbxList Of hoImap False "" "*" vFolders To iSuccess
If (iSuccess <> True) Begin
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
Showln "Could not list existing labels."
Procedure_Return
End
Get ComSeparatorChar Of hoImap To sSeparator
If (sSeparator = "") Begin
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
Showln "The server did not provide a hierarchy separator."
Procedure_Return
End
Get ComGetMailboxIndex Of hoFolders sParent To iTemp1
If (iTemp1 < 0) Begin
Get ComCreateMailbox Of hoImap sParent To iSuccess
If (iSuccess <> True) Begin
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
Showln "Could not create the parent label."
Procedure_Return
End
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoChild
If (Not(IsComObjectCreated(hoChild))) Begin
Send CreateComObject of hoChild
End
Get ComAppend Of hoChild sParent To iSuccess
Get ComAppend Of hoChild sSeparator To iSuccess
Get ComAppend Of hoChild "Child" To iSuccess
Get ComGetMailboxIndex Of hoFolders (ComGetAsString(hoChild)) To iTemp1
If (iTemp1 < 0) Begin
Get ComGetAsString Of hoChild To sTemp1
Get ComCreateMailbox Of hoImap sTemp1 To iSuccess
If (iSuccess <> True) Begin
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
Showln "Could not create the child label. The parent may already exist."
Procedure_Return
End
End
Get ComGetAsString Of hoChild To sTemp1
Showln "Labels available: " sParent " and " sTemp1
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
End_Procedure