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 PowerBuilder Downloads
integer li_rc
long li_Success
string ls_Mailbox
string ls_AppPassword
oleobject loo_Files
oleobject loo_Mailman
oleobject loo_Joined1
oleobject loo_Uidls
oleobject loo_Hash
oleobject loo_Mime
long li_Saved
long li_Skipped
long i
string ls_Uidl
oleobject loo_Joined2
string ls_FileKey
oleobject loo_Joined3
string ls_Path
long li_Exists
oleobject loo_Joined4
oleobject loo_Joined5
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Use a private directory dedicated to this application's downloaded mail.
ls_Mailbox = "user@example.com"
ls_AppPassword = "YOUR_16_CHARACTER_APP_PASSWORD"
loo_Files = create oleobject
li_rc = loo_Files.ConnectToNewObject("Chilkat.FileAccess")
if li_rc < 0 then
destroy loo_Files
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_Files.DirEnsureExists("gmail-pop-mail")
if li_Success <> 1 then
Write-Debug "Could not create the private download directory."
destroy loo_Files
return
end if
loo_Mailman = create oleobject
li_rc = loo_Mailman.ConnectToNewObject("Chilkat.MailMan")
loo_Mailman.MailHost = "pop.gmail.com"
loo_Mailman.MailPort = 995
loo_Mailman.PopSsl = 1
loo_Mailman.RequireSslCertVerify = 1
loo_Joined1 = create oleobject
li_rc = loo_Joined1.ConnectToNewObject("Chilkat.StringBuilder")
loo_Joined1.Append("recent:")
loo_Joined1.Append(ls_Mailbox)
loo_Mailman.PopUsername = loo_Joined1.GetAsString()
loo_Mailman.PopPassword = ls_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.
li_Success = loo_Mailman.Pop3BeginSession()
if li_Success <> 1 then
loo_Mailman.Pop3EndSessionNoQuit()
Write-Debug "POP login failed. Check the app password, POP setting, and account policy."
destroy loo_Files
destroy loo_Mailman
destroy loo_Joined1
return
end if
loo_Uidls = create oleobject
li_rc = loo_Uidls.ConnectToNewObject("Chilkat.StringTable")
li_Success = loo_Mailman.FetchUidls(loo_Uidls)
if li_Success <> 1 then
loo_Mailman.Pop3EndSessionNoQuit()
Write-Debug "Could not list POP UIDLs."
destroy loo_Files
destroy loo_Mailman
destroy loo_Joined1
destroy loo_Uidls
return
end if
loo_Hash = create oleobject
li_rc = loo_Hash.ConnectToNewObject("Chilkat.Crypt2")
loo_Hash.HashAlgorithm = "sha256"
loo_Hash.EncodingMode = "hex"
loo_Hash.Charset = "utf-8"
loo_Mime = create oleobject
li_rc = loo_Mime.ConnectToNewObject("Chilkat.BinData")
li_Saved = 0
li_Skipped = 0
i = 0
do while (i < loo_Uidls.Count)
ls_Uidl = loo_Uidls.StringAt(i)
// Never use the opaque server UIDL as a filesystem path.
loo_Joined2 = create oleobject
li_rc = loo_Joined2.ConnectToNewObject("Chilkat.StringBuilder")
loo_Joined2.Append(ls_Mailbox)
loo_Joined2.Append("~n")
loo_Joined2.Append(ls_Uidl)
ls_FileKey = loo_Hash.HashStringENC(loo_Joined2.GetAsString())
if loo_Hash.LastMethodSuccess <> 1 then
loo_Mailman.Pop3EndSessionNoQuit()
Write-Debug "Could not construct the local UIDL key."
destroy loo_Files
destroy loo_Mailman
destroy loo_Joined1
destroy loo_Uidls
destroy loo_Hash
destroy loo_Mime
destroy loo_Joined2
return
end if
loo_Joined3 = create oleobject
li_rc = loo_Joined3.ConnectToNewObject("Chilkat.StringBuilder")
loo_Joined3.Append("gmail-pop-mail/")
loo_Joined3.Append(ls_FileKey)
loo_Joined3.Append(".eml")
ls_Path = loo_Joined3.GetAsString()
li_Exists = loo_Files.FileExists3(ls_Path)
if li_Exists < 0 then
loo_Mailman.Pop3EndSessionNoQuit()
Write-Debug "Could not check the local download receipt."
destroy loo_Files
destroy loo_Mailman
destroy loo_Joined1
destroy loo_Uidls
destroy loo_Hash
destroy loo_Mime
destroy loo_Joined2
destroy loo_Joined3
return
end if
if li_Exists = 1 then
li_Skipped = li_Skipped + 1
else
li_Success = loo_Mailman.FetchMimeBd(ls_Uidl,loo_Mime)
if li_Success <> 1 then
loo_Mailman.Pop3EndSessionNoQuit()
Write-Debug "Download failed; the UIDL was not marked complete."
destroy loo_Files
destroy loo_Mailman
destroy loo_Joined1
destroy loo_Uidls
destroy loo_Hash
destroy loo_Mime
destroy loo_Joined2
destroy loo_Joined3
return
end if
// 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.
loo_Joined4 = create oleobject
li_rc = loo_Joined4.ConnectToNewObject("Chilkat.StringBuilder")
loo_Joined4.Append(ls_Path)
loo_Joined4.Append(".part")
li_Success = loo_Mime.WriteFile(loo_Joined4.GetAsString())
if li_Success <> 1 then
loo_Mailman.Pop3EndSessionNoQuit()
Write-Debug "Could not save the complete MIME file."
destroy loo_Files
destroy loo_Mailman
destroy loo_Joined1
destroy loo_Uidls
destroy loo_Hash
destroy loo_Mime
destroy loo_Joined2
destroy loo_Joined3
destroy loo_Joined4
return
end if
loo_Joined5 = create oleobject
li_rc = loo_Joined5.ConnectToNewObject("Chilkat.StringBuilder")
loo_Joined5.Append(ls_Path)
loo_Joined5.Append(".part")
li_Success = loo_Files.FileRename(loo_Joined5.GetAsString(),ls_Path)
if li_Success <> 1 then
loo_Mailman.Pop3EndSessionNoQuit()
Write-Debug "Could not commit the downloaded message. Keep the temporary file for diagnosis."
destroy loo_Files
destroy loo_Mailman
destroy loo_Joined1
destroy loo_Uidls
destroy loo_Hash
destroy loo_Mime
destroy loo_Joined2
destroy loo_Joined3
destroy loo_Joined4
destroy loo_Joined5
return
end if
li_Saved = li_Saved + 1
end if
i = i + 1
loop
// No DELE commands: leave the messages on Gmail for other clients/recent-mode polls.
li_Success = loo_Mailman.Pop3EndSession()
Write-Debug "Saved: " + string(li_Saved) + " Already saved: " + string(li_Skipped)
if li_Success <> 1 then
Write-Debug "The local files are committed, but closing the POP session failed."
end if
destroy loo_Files
destroy loo_Mailman
destroy loo_Joined1
destroy loo_Uidls
destroy loo_Hash
destroy loo_Mime
destroy loo_Joined2
destroy loo_Joined3
destroy loo_Joined4
destroy loo_Joined5