Sample code for 30+ languages & platforms
Objective-C

Determine the Number of Unseen Email Messages

Demonstrates how to determine how many unseen messages exist in an email account on an IMAP server.

Chilkat Objective-C Downloads

Objective-C
#import <CkoImap.h>
#import <CkoMessageSet.h>

BOOL success = NO;

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

CkoImap *imap = [[CkoImap alloc] init];

//  Connect to an IMAP server.
//  Use TLS
imap.Ssl = YES;
imap.Port = [NSNumber numberWithInt:993];
success = [imap Connect: @"imap.example.com"];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

//  Login
success = [imap Login: @"***" password: @"***"];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

//  Select an IMAP mailbox
success = [imap SelectMailbox: @"Inbox"];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

//  After selecting the mailbox. the total number of emails
//  is immediately available:
int totalNum = [imap.NumMessages intValue];
NSLog(@"%@%d",@"Num messages = ",totalNum);

//  To determine the number of unseen messages, a call
//  to Search is required, which returns the set of UIDs
//  of all unseen messages.

//  We can choose to fetch UIDs or sequence numbers.
BOOL fetchUids = YES;
CkoMessageSet *messageSet = [[CkoMessageSet alloc] init];
success = [imap QueryMbx: @"UNSEEN" bUid: fetchUids msgSet: messageSet];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

unsigned long numUnseen = [messageSet.Count intValue];
NSLog(@"%d",numUnseen);

NSLog(@"%@",@"UIDs ----");

//  Display the UIDs
int i = 0;
while (i < [messageSet.Count intValue]) {
    NSLog(@"%d",[messageSet GetId: [NSNumber numberWithInt: i]]);
    i = i + 1;
}

//  Disconnect from the IMAP server.
success = [imap Disconnect];