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

Gmail IMAP: Inspect Flags and Locate the Spam Folder

See more IMAP Examples

Inspect the current INBOX flags and discover Gmail's Spam folder through its \Junk special-use attribute. The example reads the first message's flags, if present, and makes no mailbox changes.

Client-specific Junk/NonJunk keywords are not a portable spam verdict. A mailbox's \Junk LIST attribute identifies a special folder; it is not a message flag. Use current server responses rather than a historical transcript. For Gmail label membership, see X-GM-LABELS.

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.

Reference 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 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;
}

success = [imap ExamineMailbox: @"INBOX"];
if (success != YES) {
    [imap Logout];
    [imap Disconnect];
    NSLog(@"%@",@"Could not examine INBOX.");
    return;
}

//  Capture only the EXAMINE response, after authentication has completed.
NSLog(@"%@",imap.LastResponse);
//  Sample flags without marking messages read. Sequence numbers are session-local.
if ([imap.NumMessages intValue] > 0) {
    unsigned long sequence = 1;
    NSString *flags = [imap FetchFlags: sequence bUid: NO];
    if (imap.LastMethodSuccess != YES) {
        [imap Logout];
        [imap Disconnect];
        NSLog(@"%@",@"Could not fetch flags; the message may have changed or disappeared.");
        return;
    }

    NSLog(@"%@%d%@%@",@"Sequence ",sequence,@": ",flags);
}

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

int i = 0;
while ((i < [folders.Count intValue])) {
    if ([folders HasFlag: [NSNumber numberWithInt: i] flagName: @"\\Junk"] == YES) {
        NSLog(@"%@%@",@"Spam folder: ",[folders GetName: [NSNumber numberWithInt: i]]);
    }

    i = i + 1;
}

[imap Logout];
[imap Disconnect];