POP3: Download Most Recent Email (method 2)
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 complete list of UIDLs and then downloads the last N into a bundle object. It assumes that the UIDLs will be returned by the POP3 server ordered by date such that the 1st email in the UIDL list is the oldest, and the last email is the newest.
Chilkat PureBasic Downloads
IncludeFile "CkStringTable.pb"
IncludeFile "CkEmailBundle.pb"
IncludeFile "CkEmail.pb"
IncludeFile "CkMailMan.pb"
Procedure ChilkatExample()
success.i = 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.
mailman.i = CkMailMan::ckCreate()
If mailman.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Set the POP3 server's hostname
CkMailMan::setCkMailHost(mailman, "pop.example.com")
; Set the POP3 login/password.
CkMailMan::setCkPopUsername(mailman, "bob@example.com")
CkMailMan::setCkPopPassword(mailman, "****")
; Get the complete list of UIDLs
stUidls.i = CkStringTable::ckCreate()
If stUidls.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkMailMan::ckFetchUidls(mailman,stUidls)
If success = 0
Debug CkMailMan::ckLastErrorText(mailman)
CkMailMan::ckDispose(mailman)
CkStringTable::ckDispose(stUidls)
ProcedureReturn
EndIf
; Get the 10 most recent UIDLs
; The 1st email is the oldest, the last email is the newest (usually)
startIdx.i = 0
n.i = CkStringTable::ckCount(stUidls)
If n = 0
Debug "No email in the inbox."
CkMailMan::ckDispose(mailman)
CkStringTable::ckDispose(stUidls)
ProcedureReturn
EndIf
count.i = 10
If n > 10
startIdx = n - 10
Else
startIdx = 0
EndIf
stUidls2.i = CkStringTable::ckCreate()
If stUidls2.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
endIdx.i = n - 1
i.i
For i = startIdx To endIdx
CkStringTable::ckAppend(stUidls2,CkStringTable::ckStringAt(stUidls,i))
Next
; Download in full the 10 most recent emails:
bundle.i = CkEmailBundle::ckCreate()
If bundle.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
headersOnly.i = 0
; numBodyLines is ignored when fetching full emails.
numBodyLines.i = 0
success = CkMailMan::ckFetchUidlSet(mailman,stUidls2,headersOnly,numBodyLines,bundle)
If success = 0
Debug CkMailMan::ckLastErrorText(mailman)
CkMailMan::ckDispose(mailman)
CkStringTable::ckDispose(stUidls)
CkStringTable::ckDispose(stUidls2)
CkEmailBundle::ckDispose(bundle)
ProcedureReturn
EndIf
email.i = CkEmail::ckCreate()
If email.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
i = 0
While i < CkEmailBundle::ckMessageCount(bundle)
CkEmailBundle::ckEmailAt(bundle,i,email)
Debug CkEmail::ckFrom(email)
Debug CkEmail::ckSubject(email)
Debug "----"
i = i + 1
Wend
CkMailMan::ckDispose(mailman)
CkStringTable::ckDispose(stUidls)
CkStringTable::ckDispose(stUidls2)
CkEmailBundle::ckDispose(bundle)
CkEmail::ckDispose(email)
ProcedureReturn
EndProcedure