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 Delphi DLL Downloads
var
success: Boolean;
mailbox: PWideChar;
appPassword: PWideChar;
files: HCkFileAccess;
mailman: HCkMailMan;
joined1: HCkStringBuilder;
uidls: HCkStringTable;
hash: HCkCrypt2;
mime: HCkBinData;
saved: Integer;
skipped: Integer;
i: Integer;
uidl: PWideChar;
joined2: HCkStringBuilder;
fileKey: PWideChar;
joined3: HCkStringBuilder;
path: PWideChar;
exists: Integer;
joined4: HCkStringBuilder;
joined5: HCkStringBuilder;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Use a private directory dedicated to this application's downloaded mail.
mailbox := 'user@example.com';
appPassword := 'YOUR_16_CHARACTER_APP_PASSWORD';
files := CkFileAccess_Create();
success := CkFileAccess_DirEnsureExists(files,'gmail-pop-mail');
if (success <> True) then
begin
Memo1.Lines.Add('Could not create the private download directory.');
Exit;
end;
mailman := CkMailMan_Create();
CkMailMan_putMailHost(mailman,'pop.gmail.com');
CkMailMan_putMailPort(mailman,995);
CkMailMan_putPopSsl(mailman,True);
CkMailMan_putRequireSslCertVerify(mailman,True);
joined1 := CkStringBuilder_Create();
CkStringBuilder_Append(joined1,'recent:');
CkStringBuilder_Append(joined1,mailbox);
CkMailMan_putPopUsername(mailman,CkStringBuilder__getAsString(joined1));
CkMailMan_putPopPassword(mailman,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 := CkMailMan_Pop3BeginSession(mailman);
if (success <> True) then
begin
CkMailMan_Pop3EndSessionNoQuit(mailman);
Memo1.Lines.Add('POP login failed. Check the app password, POP setting, and account policy.');
Exit;
end;
uidls := CkStringTable_Create();
success := CkMailMan_FetchUidls(mailman,uidls);
if (success <> True) then
begin
CkMailMan_Pop3EndSessionNoQuit(mailman);
Memo1.Lines.Add('Could not list POP UIDLs.');
Exit;
end;
hash := CkCrypt2_Create();
CkCrypt2_putHashAlgorithm(hash,'sha256');
CkCrypt2_putEncodingMode(hash,'hex');
CkCrypt2_putCharset(hash,'utf-8');
mime := CkBinData_Create();
saved := 0;
skipped := 0;
i := 0;
while (i < CkStringTable_getCount(uidls)) do
begin
uidl := CkStringTable__stringAt(uidls,i);
// Never use the opaque server UIDL as a filesystem path.
joined2 := CkStringBuilder_Create();
CkStringBuilder_Append(joined2,mailbox);
CkStringBuilder_Append(joined2,#10);
CkStringBuilder_Append(joined2,uidl);
fileKey := CkCrypt2__hashStringENC(hash,CkStringBuilder__getAsString(joined2));
if (CkCrypt2_getLastMethodSuccess(hash) <> True) then
begin
CkMailMan_Pop3EndSessionNoQuit(mailman);
Memo1.Lines.Add('Could not construct the local UIDL key.');
Exit;
end;
joined3 := CkStringBuilder_Create();
CkStringBuilder_Append(joined3,'gmail-pop-mail/');
CkStringBuilder_Append(joined3,fileKey);
CkStringBuilder_Append(joined3,'.eml');
path := CkStringBuilder__getAsString(joined3);
exists := CkFileAccess_FileExists3(files,path);
if (exists < 0) then
begin
CkMailMan_Pop3EndSessionNoQuit(mailman);
Memo1.Lines.Add('Could not check the local download receipt.');
Exit;
end;
if (exists = 1) then
begin
skipped := skipped + 1;
end
else
begin
success := CkMailMan_FetchMimeBd(mailman,uidl,mime);
if (success <> True) then
begin
CkMailMan_Pop3EndSessionNoQuit(mailman);
Memo1.Lines.Add('Download failed; the UIDL was not marked complete.');
Exit;
end;
// 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.
joined4 := CkStringBuilder_Create();
CkStringBuilder_Append(joined4,path);
CkStringBuilder_Append(joined4,'.part');
success := CkBinData_WriteFile(mime,CkStringBuilder__getAsString(joined4));
if (success <> True) then
begin
CkMailMan_Pop3EndSessionNoQuit(mailman);
Memo1.Lines.Add('Could not save the complete MIME file.');
Exit;
end;
joined5 := CkStringBuilder_Create();
CkStringBuilder_Append(joined5,path);
CkStringBuilder_Append(joined5,'.part');
success := CkFileAccess_FileRename(files,CkStringBuilder__getAsString(joined5),path);
if (success <> True) then
begin
CkMailMan_Pop3EndSessionNoQuit(mailman);
Memo1.Lines.Add('Could not commit the downloaded message. Keep the temporary file for diagnosis.');
Exit;
end;
saved := saved + 1;
end;
i := i + 1;
end;
// No DELE commands: leave the messages on Gmail for other clients/recent-mode polls.
success := CkMailMan_Pop3EndSession(mailman);
Memo1.Lines.Add('Saved: ' + IntToStr(saved) + ' Already saved: ' + IntToStr(skipped));
if (success <> True) then
begin
Memo1.Lines.Add('The local files are committed, but closing the POP session failed.');
end;
CkFileAccess_Dispose(files);
CkMailMan_Dispose(mailman);
CkStringBuilder_Dispose(joined1);
CkStringTable_Dispose(uidls);
CkCrypt2_Dispose(hash);
CkBinData_Dispose(mime);
CkStringBuilder_Dispose(joined2);
CkStringBuilder_Dispose(joined3);
CkStringBuilder_Dispose(joined4);
CkStringBuilder_Dispose(joined5);