Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.1.0+

Gmail IMAP: Discover Special Folders

See more GMail SMTP/IMAP/POP Examples

Find Gmail special folders from the attributes returned by LIST, using MbxList and Mailboxes.HasFlag. The result maps roles such as Sent and Trash to the server-provided mailbox names. Chilkat decodes mailbox names for use with its mailbox methods.

Folder names can be localized and visibility can differ by account. The role attributes are stable; do not hard-code [Gmail]/Sent Mail or use the deprecated XLIST command. INBOX is a protocol-defined name. Missing map keys indicate folders not advertised by the server.

Use the mailbox that authorized the full-mail scope https://mail.google.com/. Obtain the protocol OAuth token and refresh it before running this example. The token is loaded from gmail-mail-token.json in a private working directory; a narrow Gmail REST token is insufficient.

Google documentation

Chilkat Objective-C Downloads

Objective-C
#import <CkoJsonObject.h>
#import <NSString.h>
#import <CkoImap.h>
#import <CkoMailboxes.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 load the protocol token from the private working directory.");
    return;
}

if (([[token JsonTypeOf: @"access_token"] intValue] != 1) || ([token StringOfEquals: @"access_token" value: @"" caseSensitive: YES] == YES)) {
    NSLog(@"%@",@"Authorize or refresh the mailbox 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, scope, expiry, and account policy.");
    return;
}

CkoMailboxes *folders = [[CkoMailboxes alloc] init];
success = [imap MbxList: NO reference: @"" mbxPattern: @"*" mboxes: folders];
if (success != YES) {
    [imap Logout];
    [imap Disconnect];
    NSLog(@"%@",@"Could not list Gmail folders.");
    return;
}

//  LIST special-use attributes work with localized folder names. XLIST is obsolete.
CkoJsonObject *special = [[CkoJsonObject alloc] init];
int i = 0;
while ((i < [folders.Count intValue])) {
    if ([folders IsSelectable: [NSNumber numberWithInt: i]] == YES) {
        NSLog(@"%@%@%@",[folders GetName: [NSNumber numberWithInt: i]],@"  ",[folders GetFlags: [NSNumber numberWithInt: i]]);
        if ([folders HasFlag: [NSNumber numberWithInt: i] flagName: @"\\All"] == YES) {
            [special UpdateString: @"allMail" value: [folders GetName: [NSNumber numberWithInt: i]]];
        }

        if ([folders HasFlag: [NSNumber numberWithInt: i] flagName: @"\\Sent"] == YES) {
            [special UpdateString: @"sent" value: [folders GetName: [NSNumber numberWithInt: i]]];
        }

        if ([folders HasFlag: [NSNumber numberWithInt: i] flagName: @"\\Drafts"] == YES) {
            [special UpdateString: @"drafts" value: [folders GetName: [NSNumber numberWithInt: i]]];
        }

        if ([folders HasFlag: [NSNumber numberWithInt: i] flagName: @"\\Junk"] == YES) {
            [special UpdateString: @"spam" value: [folders GetName: [NSNumber numberWithInt: i]]];
        }

        if ([folders HasFlag: [NSNumber numberWithInt: i] flagName: @"\\Trash"] == YES) {
            [special UpdateString: @"trash" value: [folders GetName: [NSNumber numberWithInt: i]]];
        }

        if ([folders HasFlag: [NSNumber numberWithInt: i] flagName: @"\\Flagged"] == YES) {
            [special UpdateString: @"starred" value: [folders GetName: [NSNumber numberWithInt: i]]];
        }

        if ([folders HasFlag: [NSNumber numberWithInt: i] flagName: @"\\Important"] == YES) {
            [special UpdateString: @"important" value: [folders GetName: [NSNumber numberWithInt: i]]];
        }

    }

    i = i + 1;
}

[imap Logout];
[imap Disconnect];
special.EmitCompact = NO;
NSLog(@"%@",[special Emit]);
//  Missing keys mean the folder was not advertised. Do not substitute an English name.