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

Move IMAP Messages with OAuth2 and the MOVE Extension

See more IMAP Examples

Move matching messages between two existing mailboxes using QueryMbx and MoveMessages. Gmail OAuth2 supplies the concrete connection example. Other servers require their own authentication settings.

This changes all matching messages in the source folder. Choose the source, destination, and search criterion before running it. Gmail custom folders represent labels; moving between them changes label membership. The code checks MOVE at runtime, handles an empty search, and uses UID-based message sets. It does not fall back to copy/delete/EXPUNGE or print authentication logs. Confirm the result before retrying after a lost connection.

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

NSString *capabilities = [imap Capability];
if ((imap.LastMethodSuccess != YES) || ([imap HasCapability: @"MOVE" capabilityResponse: capabilities] != YES)) {
    [imap Logout];
    [imap Disconnect];
    NSLog(@"%@",@"This example requires the MOVE capability.");
    return;
}

//  These existing custom-label folders are inputs. The operation moves matching messages.
NSString *sourceFolder = @"Example";
NSString *destinationFolder = @"Example/Processed";
success = [imap SelectMailbox: sourceFolder];
if (success != YES) {
    [imap Logout];
    [imap Disconnect];
    NSLog(@"%@",@"Could not select the source folder.");
    return;
}

CkoMessageSet *selected = [[CkoMessageSet alloc] init];
success = [imap QueryMbx: @"FROM sender@example.net" bUid: YES msgSet: selected];
if (success != YES) {
    [imap Logout];
    [imap Disconnect];
    NSLog(@"%@",@"Could not search the source folder.");
    return;
}

if ([selected.Count intValue] == 0) {
    [imap Logout];
    [imap Disconnect];
    NSLog(@"%@",@"No matching messages; nothing was moved.");
    return;
}

NSLog(@"%@%@",@"Moving UID set: ",[selected ToCompactString]);
success = [imap MoveMessages: selected destFolder: destinationFolder];
if (success != YES) {
    [imap Logout];
    [imap Disconnect];
    NSLog(@"%@",@"Move failed. Verify the outcome before retrying an interrupted operation.");
    return;
}

NSLog(@"%@",@"MOVE completed for the selected messages.");
[imap Logout];
[imap Disconnect];