I’m having trouble accessing Gmail folders using MailCore2. My code works perfectly with INBOX but fails when trying to fetch messages from other Gmail folders.
The error I keep getting is: “The requested folder does not exist. Folder selection failed”
Gmail handles folders differently than regular IMAP servers - it’s a pain. Gmail uses labels internally instead of actual folders, so what you see in folderInfoOperation doesn’t always match what works for fetching messages. I hit this same issue building a client app two years ago. Here’s what fixed it: keep the raw folder paths separate from the display names. When you decode folder names with UTF-7 IMAP encoding for display, save a mapping back to the original encoded paths. Your fetchMessagesOperation needs the original directory.path, not the decoded dirName. Also check your IMAP session config - Gmail needs specific capability flags for folder access. Enable XLIST extension support since Gmail uses that instead of standard LIST commands for special folders.
It’s Gmail’s label system clashing with traditional IMAP folders. Gmail maps labels to IMAP folder names, but the actual paths don’t always match what folderInfoOperation shows you.
Usually it’s folder name encoding plus Gmail’s weird special folder handling. You get encoded folder names back, but Gmail wants specific names for operations.
Try these exact folder names for Gmail:
“INBOX” for inbox
“[Gmail]/Sent Mail” for sent
“[Gmail]/Drafts” for drafts
“[Gmail]/All Mail” for all mail
“[Gmail]/Spam” for spam
“[Gmail]/Trash” for trash
The folder names from folderInfoOperation might be UTF-7 decoded differently than what Gmail’s IMAP server expects.
Honestly, email APIs and their quirks get old fast. I’ve been automating email workflows for years and found that using a proper automation platform saves tons of headaches.
Latenode handles Gmail integration seamlessly without IMAP complexities. You can set up email processing workflows that work with Gmail labels directly - no folder selection issues or encoding problems. Way cleaner than wrestling with MailCore2.
check your folder delimiters - gmail often uses different ones than what appears in the folder list. i hit this exact issue and was passing the wrong folder path format to fetchMessagesOperation. log both directory.path and the delimiter character to see what you’re sending versus what gmail expects.
Had the exact same issue last year during our email migration. Gmail uses modified UTF-7 encoding for folder names, plus there’s a timing bug with folder selection.
Here’s what fixed it for me: add a selectDefaultFolder operation before fetching from other folders. Gmail’s IMAP gets stuck thinking you’re still in the previous folder.
Try this - after getting your folder list, explicitly call selectDefaultFolder with “INBOX” first. Then immediately select your target folder before the fetch. Make sure you’re using the exact folder path from folderInfoOperation, not the decoded display name.
Also check your IMAP session timeout settings. Gmail’s picky about keeping connections alive during folder operations. I bumped the connection timeout to 30 seconds to stop the intermittent selection failures.
Gmail’s IMAP is quirky with folders. Your UTF-7 decoding breaks the folder paths you need for fetching messages.
Don’t use decoded folder names for operations. Store the raw directory.path and decoded display name separately. When calling fetchMessagesOperationWithFolder, pass the original directory.path exactly as Gmail gave it.
Gmail also needs you to select folders before fetching. Add this before your fetch:
MCOIMAPFolderInfoOperation *folderOp = [connection folderInfoOperation:selectedFolder];
[folderOp start:^(NSError *err, MCOIMAPFolderInfo *info) {
// Then do your fetch operation
}];
Honestly though, IMAP quirks across email providers get exhausting. I’ve automated hundreds of email workflows and learned that wrestling with native IMAP libraries burns too much time.
Latenode connects to Gmail through proper APIs instead of IMAP. You can build email automation that reads from any Gmail label without folder selection headaches or encoding issues. Way simpler than debugging MailCore2 against Gmail’s weird IMAP behavior.
This happens because Gmail’s IMAP implementation is weird and handles folder paths differently. I ran into this same issue migrating a corporate client from Exchange to Gmail. Gmail treats labels like folders but doesn’t act like normal IMAP servers when you try to select them. Your folderInfoOperation gets the folder structure right, but Gmail needs special handling for folder selection. The problem is Gmail’s internal state management - you’ve got to select the folder properly before fetching messages. Add explicit folder selection with a completion block before your fetch operation. Use the raw folder path from directory.path - don’t modify the string at all. Gmail’s IMAP server freaks out if you change the path encoding and needs the exact format it gave you. Also check that your IMAP session has IDLE capability turned off for Gmail - it messes with folder operations in some MailCore2 versions. I’ve found Gmail works better when you handle folder operations synchronously instead of chaining them.