POP3: Download Most Recent Email (method 1)
How to ready the N most recent email from a POP3 server.The POP3 protocol does not provide the ability to request the most recent email, nor does it provide the ability to download email based on read/unread status or any other criteria. The design principle behind POP3 is that it is a temporary holding store for incoming email and email clients or other applications will transfer email from the POP3 server to a local persistent store where it will be managed. Therefore, POP3 does not provide sophisticated functionality (as opposed to IMAP which has the opposite design philosophy: that email is maintained and organized on the server).
This example shows one possible way to retrieve the N most recent emails from a POP3 server. It downloads the headers into a bundle object, then sorts the bundle by date, and finally retrieves the most recent emails by UIDL.
Another alternative is to download the UIDL list and then assume that the 1st N UIDLs are the most recent.
Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Mailman
oleobject loo_Bundle
integer li_KeepOnServer
integer li_HeadersOnly
integer li_NumBodyLines
integer li_Ascending
oleobject loo_StUidls
integer n
oleobject loo_Email
integer i
oleobject loo_Bundle2
li_Success = 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The mailman object is used for receiving (POP3)
// and sending (SMTP) email.
loo_Mailman = create oleobject
li_rc = loo_Mailman.ConnectToNewObject("Chilkat.MailMan")
if li_rc < 0 then
destroy loo_Mailman
MessageBox("Error","Connecting to COM object failed")
return
end if
// Set the POP3 server's hostname
loo_Mailman.MailHost = "pop.example.com"
// Set the POP3 login/password.
loo_Mailman.PopUsername = "myLogin"
loo_Mailman.PopPassword = "myPassword"
// Read mail headers and one line of the body.
loo_Bundle = create oleobject
li_rc = loo_Bundle.ConnectToNewObject("Chilkat.EmailBundle")
li_KeepOnServer = 1
li_HeadersOnly = 1
li_NumBodyLines = 1
li_Success = loo_Mailman.FetchAll(li_KeepOnServer,li_HeadersOnly,li_NumBodyLines,loo_Bundle)
if li_Success = 0 then
Write-Debug loo_Mailman.LastErrorText
destroy loo_Mailman
destroy loo_Bundle
return
end if
// Sort the bundle by date
li_Ascending = 0
loo_Bundle.SortByDate(li_Ascending)
// Get the 10 most recent UIDLs
loo_StUidls = create oleobject
li_rc = loo_StUidls.ConnectToNewObject("Chilkat.StringTable")
n = loo_Bundle.MessageCount
if n > 10 then
n = 10
end if
loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")
i = 0
do while i < n
loo_Bundle.EmailAt(i,loo_Email)
li_Success = loo_StUidls.Append(loo_Email.Uidl)
i = i + 1
loop
// Download in full the 10 most recent emails:
loo_Bundle2 = create oleobject
li_rc = loo_Bundle2.ConnectToNewObject("Chilkat.EmailBundle")
li_HeadersOnly = 0
// numBodyLines is ignored if we're fetching the full emails.
li_Success = loo_Mailman.FetchUidlSet(loo_StUidls,li_HeadersOnly,li_NumBodyLines,loo_Bundle2)
if li_Success = 0 then
Write-Debug loo_Mailman.LastErrorText
destroy loo_Mailman
destroy loo_Bundle
destroy loo_StUidls
destroy loo_Email
destroy loo_Bundle2
return
end if
i = 0
do while i < loo_Bundle2.MessageCount
loo_Bundle2.EmailAt(i,loo_Email)
Write-Debug loo_Email.From
Write-Debug loo_Email.Subject
Write-Debug "----"
i = i + 1
loop
destroy loo_Mailman
destroy loo_Bundle
destroy loo_StUidls
destroy loo_Email
destroy loo_Bundle2