MailCore2 Gmail folder access issues

I’m having trouble accessing Gmail folders using MailCore2. The INBOX folder works perfectly but when I try to fetch messages from other folders I get this error: “The requested folder does not exist. Folder selection failed”. This same code works fine with other email providers but Gmail seems to be different. Here’s my code for getting the folder list:

_emailSession = [EmailHelper getIMAPConnection];

[[_emailSession getAllFoldersOperation] start:^(NSError *err, NSArray *allFolders) {
    [[SharedFolders instance].directories removeAllObjects];
    for (MCOIMAPFolder *directory in allFolders) {
        NSArray *parts = [directory.path componentsSeparatedByString:[NSString stringWithFormat:@"%c", directory.delimiter]];
        NSString *dirName = [parts lastObject];
        const char *charString = [dirName cStringUsingEncoding:[NSString defaultCStringEncoding]];
        dirName = [NSString stringWithCString:charString encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF7_IMAP)];
        [directories addObject:dirName];
        NSLog(@"Found folder: %@", dirName);
    }
}];

I can see all folders including Gmail-specific ones like “Sent Mail”, “All Mail”, “Trash” etc. But when I try to fetch messages from these folders:

MCOIMAPSession *connection = [EmailHelper getIMAPConnection];
if (!connection) return;

MCOIMAPMessagesRequestKind kind = MCOIMAPMessagesRequestKindHeaders;
MCOIndexSet *messageIds = [MCOIndexSet indexSetWithRange:MCORangeMake(1, UINT64_MAX)];

MCOIMAPFetchMessagesOperation *operation = [connection fetchMessagesOperationWithFolder:selectedFolder requestKind:kind uids:messageIds];
[operation start:^(NSError *err, NSArray *messages, MCOIndexSet *removedMessages) {
    if(err) {
        NSLog(@"Failed to get messages: %@", err);
        if (callback) callback(NO);
    }

Only INBOX works but other folders throw the “folder does not exist” error. What am I missing for Gmail folders?

I encountered a similar issue with Gmail folder access. The problem lies in the way Gmail names its folders; they often include prefixes like “[Gmail]/” that are crucial for identifying the folder correctly. Instead of extracting the last part of the path, try using the full folder name as returned by the getAllFoldersOperation. This will allow you to access folders like “[Gmail]/Sent Mail” directly without the need for additional encoding conversion, which might also be causing issues. Be mindful of the encoding, as Gmail may return paths that can be inconsistent.

Your UTF-7 IMAP encoding is breaking Gmail’s folder names. Gmail needs the exact folder paths - don’t convert them at all. Use the original directory.path value instead of the converted dirName. Skip the UTF-7 conversion completely and pass the raw folder path straight to fetchMessagesOperationWithFolder. Gmail’s folder structure is rigid and any changes to the original path will break folder selection. I spent hours debugging this same issue before realizing the encoding was corrupting the folder references.

You’re using the processed folder name instead of the original path when fetching messages. Gmail folders need their full path with delimiters - use directory.path directly in your fetch operation, not the dirName you extracted. Also, make sure your folder selection happens before the fetch starts.