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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
String sMailbox
String sAppPassword
Handle hoFiles
Handle hoMailman
Handle hoJoined1
Variant vUidls
Handle hoUidls
Handle hoHash
Variant vMime
Handle hoMime
Integer iSaved
Integer iSkipped
Integer i
String sUidl
Handle hoJoined2
String sFileKey
Handle hoJoined3
String sPath
Integer iExists
Handle hoJoined4
Handle hoJoined5
String sTemp1
Boolean bTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Use a private directory dedicated to this application's downloaded mail.
Move "user@example.com" To sMailbox
Move "YOUR_16_CHARACTER_APP_PASSWORD" To sAppPassword
Get Create (RefClass(cComCkFileAccess)) To hoFiles
If (Not(IsComObjectCreated(hoFiles))) Begin
Send CreateComObject of hoFiles
End
Get ComDirEnsureExists Of hoFiles "gmail-pop-mail" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not create the private download directory."
Procedure_Return
End
Get Create (RefClass(cComChilkatMailMan)) To hoMailman
If (Not(IsComObjectCreated(hoMailman))) Begin
Send CreateComObject of hoMailman
End
Set ComMailHost Of hoMailman To "pop.gmail.com"
Set ComMailPort Of hoMailman To 995
Set ComPopSsl Of hoMailman To True
Set ComRequireSslCertVerify Of hoMailman To True
Get Create (RefClass(cComChilkatStringBuilder)) To hoJoined1
If (Not(IsComObjectCreated(hoJoined1))) Begin
Send CreateComObject of hoJoined1
End
Get ComAppend Of hoJoined1 "recent:" To iSuccess
Get ComAppend Of hoJoined1 sMailbox To iSuccess
Get ComGetAsString Of hoJoined1 To sTemp1
Set ComPopUsername Of hoMailman To sTemp1
Set ComPopPassword Of hoMailman To sAppPassword
// 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.
Get ComPop3BeginSession Of hoMailman To iSuccess
If (iSuccess <> True) Begin
Get ComPop3EndSessionNoQuit Of hoMailman To iSuccess
Showln "POP login failed. Check the app password, POP setting, and account policy."
Procedure_Return
End
Get Create (RefClass(cComChilkatStringTable)) To hoUidls
If (Not(IsComObjectCreated(hoUidls))) Begin
Send CreateComObject of hoUidls
End
Get pvComObject of hoUidls to vUidls
Get ComFetchUidls Of hoMailman vUidls To iSuccess
If (iSuccess <> True) Begin
Get ComPop3EndSessionNoQuit Of hoMailman To iSuccess
Showln "Could not list POP UIDLs."
Procedure_Return
End
Get Create (RefClass(cComChilkatCrypt2)) To hoHash
If (Not(IsComObjectCreated(hoHash))) Begin
Send CreateComObject of hoHash
End
Set ComHashAlgorithm Of hoHash To "sha256"
Set ComEncodingMode Of hoHash To "hex"
Set ComCharset Of hoHash To "utf-8"
Get Create (RefClass(cComChilkatBinData)) To hoMime
If (Not(IsComObjectCreated(hoMime))) Begin
Send CreateComObject of hoMime
End
Move 0 To iSaved
Move 0 To iSkipped
Move 0 To i
While (i < (ComCount(hoUidls)))
Get ComStringAt Of hoUidls i To sUidl
// Never use the opaque server UIDL as a filesystem path.
Get Create (RefClass(cComChilkatStringBuilder)) To hoJoined2
If (Not(IsComObjectCreated(hoJoined2))) Begin
Send CreateComObject of hoJoined2
End
Get ComAppend Of hoJoined2 sMailbox To iSuccess
Get ComAppend Of hoJoined2 (character(10)) To iSuccess
Get ComAppend Of hoJoined2 sUidl To iSuccess
Get ComGetAsString Of hoJoined2 To sTemp1
Get ComHashStringENC Of hoHash sTemp1 To sFileKey
Get ComLastMethodSuccess Of hoHash To bTemp1
If (bTemp1 <> True) Begin
Get ComPop3EndSessionNoQuit Of hoMailman To iSuccess
Showln "Could not construct the local UIDL key."
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoJoined3
If (Not(IsComObjectCreated(hoJoined3))) Begin
Send CreateComObject of hoJoined3
End
Get ComAppend Of hoJoined3 "gmail-pop-mail/" To iSuccess
Get ComAppend Of hoJoined3 sFileKey To iSuccess
Get ComAppend Of hoJoined3 ".eml" To iSuccess
Get ComGetAsString Of hoJoined3 To sPath
Get ComFileExists3 Of hoFiles sPath To iExists
If (iExists < 0) Begin
Get ComPop3EndSessionNoQuit Of hoMailman To iSuccess
Showln "Could not check the local download receipt."
Procedure_Return
End
If (iExists = 1) Begin
Move (iSkipped + 1) To iSkipped
End
Else Begin
Get pvComObject of hoMime to vMime
Get ComFetchMimeBd Of hoMailman sUidl vMime To iSuccess
If (iSuccess <> True) Begin
Get ComPop3EndSessionNoQuit Of hoMailman To iSuccess
Showln "Download failed; the UIDL was not marked complete."
Procedure_Return
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.
Get Create (RefClass(cComChilkatStringBuilder)) To hoJoined4
If (Not(IsComObjectCreated(hoJoined4))) Begin
Send CreateComObject of hoJoined4
End
Get ComAppend Of hoJoined4 sPath To iSuccess
Get ComAppend Of hoJoined4 ".part" To iSuccess
Get ComGetAsString Of hoJoined4 To sTemp1
Get ComWriteFile Of hoMime sTemp1 To iSuccess
If (iSuccess <> True) Begin
Get ComPop3EndSessionNoQuit Of hoMailman To iSuccess
Showln "Could not save the complete MIME file."
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoJoined5
If (Not(IsComObjectCreated(hoJoined5))) Begin
Send CreateComObject of hoJoined5
End
Get ComAppend Of hoJoined5 sPath To iSuccess
Get ComAppend Of hoJoined5 ".part" To iSuccess
Get ComGetAsString Of hoJoined5 To sTemp1
Get ComFileRename Of hoFiles sTemp1 sPath To iSuccess
If (iSuccess <> True) Begin
Get ComPop3EndSessionNoQuit Of hoMailman To iSuccess
Showln "Could not commit the downloaded message. Keep the temporary file for diagnosis."
Procedure_Return
End
Move (iSaved + 1) To iSaved
End
Move (i + 1) To i
Loop
// No DELE commands: leave the messages on Gmail for other clients/recent-mode polls.
Get ComPop3EndSession Of hoMailman To iSuccess
Showln "Saved: " iSaved " Already saved: " iSkipped
If (iSuccess <> True) Begin
Showln "The local files are committed, but closing the POP session failed."
End
End_Procedure