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 Objective-C Downloads
#import <CkoJsonObject.h>
#import <NSString.h>
#import <CkoImap.h>
#import <CkoMailboxes.h>
#import <CkoStringBuilder.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkoJsonObject *token = [[CkoJsonObject alloc] init];
success = [token LoadFile: @"gmail-mail-token.json"];
if (success != YES) {
NSLog(@"%@",@"Could not read gmail-mail-token.json from the private working directory.");
return;
}
if (([[token JsonTypeOf: @"access_token"] intValue] != 1) || ([token StringOfEquals: @"access_token" value: @"" caseSensitive: YES] == YES)) {
NSLog(@"%@",@"An OAuth access token is required. Authorize or refresh the account first.");
return;
}
NSString *mailbox = @"user@example.com";
CkoImap *imap = [[CkoImap alloc] init];
imap.Ssl = YES;
imap.Port = [NSNumber numberWithInt:993];
imap.RequireSslCertVerify = YES;
imap.AuthMethod = @"XOAUTH2";
success = [imap Connect: @"imap.gmail.com"];
if (success != YES) {
NSLog(@"%@",@"Could not establish the Gmail IMAP TLS connection.");
return;
}
success = [imap Login: mailbox password: [token StringOf: @"access_token"]];
if (success != YES) {
[imap Disconnect];
NSLog(@"%@",@"IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy.");
return;
}
// This creates a custom Gmail label and one nested label.
NSString *parent = @"Example";
CkoMailboxes *folders = [[CkoMailboxes alloc] init];
success = [imap MbxList: NO reference: @"" mbxPattern: @"*" mboxes: folders];
if (success != YES) {
[imap Logout];
[imap Disconnect];
NSLog(@"%@",@"Could not list existing labels.");
return;
}
NSString *separator = imap.SeparatorChar;
if (separator == @"") {
[imap Logout];
[imap Disconnect];
NSLog(@"%@",@"The server did not provide a hierarchy separator.");
return;
}
if ([[folders GetMailboxIndex: parent] intValue] < 0) {
success = [imap CreateMailbox: parent];
if (success != YES) {
[imap Logout];
[imap Disconnect];
NSLog(@"%@",@"Could not create the parent label.");
return;
}
}
CkoStringBuilder *child = [[CkoStringBuilder alloc] init];
[child Append: parent];
[child Append: separator];
[child Append: @"Child"];
if ([[folders GetMailboxIndex: [child GetAsString]] intValue] < 0) {
success = [imap CreateMailbox: [child GetAsString]];
if (success != YES) {
[imap Logout];
[imap Disconnect];
NSLog(@"%@",@"Could not create the child label. The parent may already exist.");
return;
}
}
NSLog(@"%@%@%@%@",@"Labels available: ",parent,@" and ",[child GetAsString]);
[imap Logout];
[imap Disconnect];