Sample code for 30+ languages & platforms
Objective-C

Refresh an Email Object's IMAP Flags

See more IMAP Examples

Demonstrates the Chilkat Imap.RefetchMailFlags method, which updates the flags stored on an Email object to the current values on the server. The only argument is the Email object. This example re-reads the flags for a previously fetched message and then checks its Seen state.

Background: Flags are shared mailbox state, so between the time you fetch a message and the time you act on it, another client (a phone, a webmail tab) may have marked it read, flagged, or deleted. RefetchMailFlags pulls just the flags — not the whole message — so a long-running job can cheaply re-check the live state before deciding what to do, avoiding acting on stale information.

Chilkat Objective-C Downloads

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

BOOL success = NO;

//  Demonstrates the Imap.RefetchMailFlags method, which updates the flags stored on an Email
//  object to the current values on the server.  The only argument is the Email object.

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

imap.Ssl = YES;
imap.Port = [NSNumber numberWithInt:993];

success = [imap Connect: @"imap.example.com"];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

success = [imap Login: @"user@example.com" password: @"myPassword"];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

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

CkoMessageSet *msgSet = [[CkoMessageSet alloc] init];
success = [imap QueryMbx: @"ALL" bUid: YES msgSet: msgSet];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

CkoEmailBundle *bundle = [[CkoEmailBundle alloc] init];
success = [imap FetchMsgSet: YES msgSet: msgSet bundle: bundle];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

CkoEmail *email = [[CkoEmail alloc] init];
if ([bundle.MessageCount intValue] > 0) {
    success = [bundle EmailAt: [NSNumber numberWithInt: 0] email: email];

    //  Re-read the flags from the server, in case another client changed them since the fetch.
    success = [imap RefetchMailFlags: email];
    if (success == NO) {
        NSLog(@"%@",imap.LastErrorText);
        return;
    }

    int seen = [[imap GetMailFlag: email flagName: @"Seen"] intValue];
    NSLog(@"%@%d",@"Current Seen state: ",seen);
}

success = [imap Disconnect];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}