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 Unicode C++ Downloads
#include <CkJsonObjectW.h>
#include <CkImapW.h>
#include <CkMailboxesW.h>
#include <CkStringBuilderW.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkJsonObjectW token;
success = token.LoadFile(L"gmail-mail-token.json");
if (success != true) {
wprintf(L"Could not read gmail-mail-token.json from the private working directory.\n");
return;
}
if ((token.JsonTypeOf(L"access_token") != 1) || (token.StringOfEquals(L"access_token",L"",true) == true)) {
wprintf(L"An OAuth access token is required. Authorize or refresh the account first.\n");
return;
}
const wchar_t *mailbox = L"user@example.com";
CkImapW imap;
imap.put_Ssl(true);
imap.put_Port(993);
imap.put_RequireSslCertVerify(true);
imap.put_AuthMethod(L"XOAUTH2");
success = imap.Connect(L"imap.gmail.com");
if (success != true) {
wprintf(L"Could not establish the Gmail IMAP TLS connection.\n");
return;
}
success = imap.Login(mailbox,token.stringOf(L"access_token"));
if (success != true) {
imap.Disconnect();
wprintf(L"IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy.\n");
return;
}
// This creates a custom Gmail label and one nested label.
const wchar_t *parent = L"Example";
CkMailboxesW folders;
success = imap.MbxList(false,L"",L"*",folders);
if (success != true) {
imap.Logout();
imap.Disconnect();
wprintf(L"Could not list existing labels.\n");
return;
}
const wchar_t *separator = imap.separatorChar();
if (separator == L"") {
imap.Logout();
imap.Disconnect();
wprintf(L"The server did not provide a hierarchy separator.\n");
return;
}
if (folders.GetMailboxIndex(parent) < 0) {
success = imap.CreateMailbox(parent);
if (success != true) {
imap.Logout();
imap.Disconnect();
wprintf(L"Could not create the parent label.\n");
return;
}
}
CkStringBuilderW child;
child.Append(parent);
child.Append(separator);
child.Append(L"Child");
if (folders.GetMailboxIndex(child.getAsString()) < 0) {
success = imap.CreateMailbox(child.getAsString());
if (success != true) {
imap.Logout();
imap.Disconnect();
wprintf(L"Could not create the child label. The parent may already exist.\n");
return;
}
}
wprintf(L"Labels available: %s and %s\n",parent,child.getAsString());
imap.Logout();
imap.Disconnect();
}