Gmail POP3: Recent Mode with Persistent UIDL Tracking
See more GMail SMTP/IMAP/POP Examples
Use recent:user@example.com to access Gmail's recent POP delivery window, then download each UIDL only once. This example uses an app password, requiring 2-Step Verification and an account policy that allows app passwords. Enable POP and select Keep Gmail's copy in the Inbox. An ordinary account password will not work. If app passwords are unavailable, use the OAuth IMAP examples.
Recent mode exposes approximately the last 30 days of mail and is useful when multiple POP clients need the same messages. Keep the recent prefix out of SMTP settings. This is a rolling window, not a complete-mailbox backup; polling after a long gap can miss older mail.
UIDLs stay as opaque strings. A SHA-256 filename derived from mailbox plus UIDL identifies each saved MIME file. The final file doubles as the persistent completion receipt: a second run skips it. A partial download or failed write is never marked complete, and a leftover .part file is retried. The code sends no DELE commands.
Keep this private directory dedicated to the downloader, use a stable mailbox spelling, and run only one writer at a time. Moving or deleting final files discards the tracking information and permits a new download while the message is still available. For business processing beyond saving MIME, use an application transaction and idempotent handling.
Chilkat Objective-C Downloads
#import <NSString.h>
#import <CkoFileAccess.h>
#import <CkoMailMan.h>
#import <CkoStringBuilder.h>
#import <CkoStringTable.h>
#import <CkoCrypt2.h>
#import <CkoBinData.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Use a private directory dedicated to this application's downloaded mail.
NSString *mailbox = @"user@example.com";
NSString *appPassword = @"YOUR_16_CHARACTER_APP_PASSWORD";
CkoFileAccess *files = [[CkoFileAccess alloc] init];
success = [files DirEnsureExists: @"gmail-pop-mail"];
if (success != YES) {
NSLog(@"%@",@"Could not create the private download directory.");
return;
}
CkoMailMan *mailman = [[CkoMailMan alloc] init];
mailman.MailHost = @"pop.gmail.com";
mailman.MailPort = [NSNumber numberWithInt:995];
mailman.PopSsl = YES;
mailman.RequireSslCertVerify = YES;
CkoStringBuilder *joined1 = [[CkoStringBuilder alloc] init];
[joined1 Append: @"recent:"];
[joined1 Append: mailbox];
mailman.PopUsername = [joined1 GetAsString];
mailman.PopPassword = appPassword;
// Use an eligible account's app password loaded from protected configuration.
// The recent: prefix is only for POP; never add it to an SMTP username.
success = [mailman Pop3BeginSession];
if (success != YES) {
[mailman Pop3EndSessionNoQuit];
NSLog(@"%@",@"POP login failed. Check the app password, POP setting, and account policy.");
return;
}
CkoStringTable *uidls = [[CkoStringTable alloc] init];
success = [mailman FetchUidls: uidls];
if (success != YES) {
[mailman Pop3EndSessionNoQuit];
NSLog(@"%@",@"Could not list POP UIDLs.");
return;
}
CkoCrypt2 *hash = [[CkoCrypt2 alloc] init];
hash.HashAlgorithm = @"sha256";
hash.EncodingMode = @"hex";
hash.Charset = @"utf-8";
CkoBinData *mime = [[CkoBinData alloc] init];
int saved = 0;
int skipped = 0;
int i = 0;
while ((i < [uidls.Count intValue])) {
NSString *uidl = [uidls StringAt: [NSNumber numberWithInt: i]];
// Never use the opaque server UIDL as a filesystem path.
CkoStringBuilder *joined2 = [[CkoStringBuilder alloc] init];
[joined2 Append: mailbox];
[joined2 Append: @"\n"];
[joined2 Append: uidl];
NSString *fileKey = [hash HashStringENC: [joined2 GetAsString]];
if (hash.LastMethodSuccess != YES) {
[mailman Pop3EndSessionNoQuit];
NSLog(@"%@",@"Could not construct the local UIDL key.");
return;
}
CkoStringBuilder *joined3 = [[CkoStringBuilder alloc] init];
[joined3 Append: @"gmail-pop-mail/"];
[joined3 Append: fileKey];
[joined3 Append: @".eml"];
NSString *path = [joined3 GetAsString];
int exists = [[files FileExists3: path] intValue];
if (exists < 0) {
[mailman Pop3EndSessionNoQuit];
NSLog(@"%@",@"Could not check the local download receipt.");
return;
}
if (exists == 1) {
skipped = skipped + 1;
}
else {
success = [mailman FetchMimeBd: uidl mimeData: mime];
if (success != YES) {
[mailman Pop3EndSessionNoQuit];
NSLog(@"%@",@"Download failed; the UIDL was not marked complete.");
return;
}
// A final .eml file is the persistent completion receipt for this UIDL.
// Save to a sibling temporary file first. A leftover .part file is not a receipt.
CkoStringBuilder *joined4 = [[CkoStringBuilder alloc] init];
[joined4 Append: path];
[joined4 Append: @".part"];
success = [mime WriteFile: [joined4 GetAsString]];
if (success != YES) {
[mailman Pop3EndSessionNoQuit];
NSLog(@"%@",@"Could not save the complete MIME file.");
return;
}
CkoStringBuilder *joined5 = [[CkoStringBuilder alloc] init];
[joined5 Append: path];
[joined5 Append: @".part"];
success = [files FileRename: [joined5 GetAsString] newPath: path];
if (success != YES) {
[mailman Pop3EndSessionNoQuit];
NSLog(@"%@",@"Could not commit the downloaded message. Keep the temporary file for diagnosis.");
return;
}
saved = saved + 1;
}
i = i + 1;
}
// No DELE commands: leave the messages on Gmail for other clients/recent-mode polls.
success = [mailman Pop3EndSession];
NSLog(@"%@%d%@%d",@"Saved: ",saved,@" Already saved: ",skipped);
if (success != YES) {
NSLog(@"%@",@"The local files are committed, but closing the POP session failed.");
}