Sample code for 30+ languages & platforms
Objective-C

Expunge Deleted Messages and Close the Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.ExpungeAndClose method, which permanently removes all messages marked with \Deleted from the selected mailbox and then closes the mailbox. This example marks a message deleted and then expunges-and-closes.

Background: This combines two actions into one call: the permanent removal of \Deleted messages (see Expunge) followed by closing the mailbox (see CloseMailbox), returning the session to the authenticated-but-no-mailbox-selected state while staying connected. It's the natural way to finish working with a mailbox after cleaning it up, before selecting another or logging out.

Chilkat Objective-C Downloads

Objective-C
#import <CkoImap.h>

BOOL success = NO;

//  Demonstrates the Imap.ExpungeAndClose method, which permanently removes all messages
//  marked with \Deleted from the selected mailbox and then closes the mailbox.

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

//  Mark message (sequence number 1) with the \Deleted flag.
success = [imap SetFlag: 1 bUid: NO flagName: @"\Deleted" value: [NSNumber numberWithInt: 1]];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

//  Remove the \Deleted messages and close the mailbox in one step.
success = [imap ExpungeAndClose];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

NSLog(@"%@",@"Expunged and closed the mailbox.");

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