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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoMailman
Variant vStUidls
Handle hoStUidls
Integer iStartIdx
Integer n
Integer iCount
Variant vStUidls
2 Handle hoStUidls2
Integer iEndIdx
Integer i
Variant vBundle
Handle hoBundle
Boolean iHeadersOnly
Integer iNumBodyLines
Variant vEmail
Handle hoEmail
String sTemp1
Move False To iSuccess
// 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.
Get Create (RefClass(cComChilkatMailMan)) To hoMailman
If (Not(IsComObjectCreated(hoMailman))) Begin
Send CreateComObject of hoMailman
End
// Set the POP3 server's hostname
Set ComMailHost Of hoMailman To "pop.example.com"
// Set the POP3 login/password.
Set ComPopUsername Of hoMailman To "bob@example.com"
Set ComPopPassword Of hoMailman To "****"
// Get the complete list of UIDLs
Get Create (RefClass(cComChilkatStringTable)) To hoStUidls
If (Not(IsComObjectCreated(hoStUidls))) Begin
Send CreateComObject of hoStUidls
End
Get pvComObject of hoStUidls to vStUidls
Get ComFetchUidls Of hoMailman vStUidls To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoMailman To sTemp1
Showln sTemp1
Procedure_Return
End
// Get the 10 most recent UIDLs
// The 1st email is the oldest, the last email is the newest (usually)
Move 0 To iStartIdx
Get ComCount Of hoStUidls To n
If (n = 0) Begin
Showln "No email in the inbox."
Procedure_Return
End
Move 10 To iCount
If (n > 10) Begin
Move (n - 10) To iStartIdx
End
Else Begin
Move 0 To iStartIdx
End
Get Create (RefClass(cComChilkatStringTable)) To hoStUidls2
If (Not(IsComObjectCreated(hoStUidls2))) Begin
Send CreateComObject of hoStUidls2
End
Move (n - 1) To iEndIdx
For i From iStartIdx To iEndIdx
Get ComStringAt Of hoStUidls i To sTemp1
Get ComAppend Of hoStUidls2 sTemp1 To iSuccess
Loop
// Download in full the 10 most recent emails:
Get Create (RefClass(cComChilkatEmailBundle)) To hoBundle
If (Not(IsComObjectCreated(hoBundle))) Begin
Send CreateComObject of hoBundle
End
Move False To iHeadersOnly
// numBodyLines is ignored when fetching full emails.
Move 0 To iNumBodyLines
Get pvComObject of hoStUidls2 to vStUidls2
Get pvComObject of hoBundle to vBundle
Get ComFetchUidlSet Of hoMailman vStUidls2 iHeadersOnly iNumBodyLines vBundle To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoMailman To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatEmail)) To hoEmail
If (Not(IsComObjectCreated(hoEmail))) Begin
Send CreateComObject of hoEmail
End
Move 0 To i
While (i < (ComMessageCount(hoBundle)))
Get pvComObject of hoEmail to vEmail
Get ComEmailAt Of hoBundle i vEmail To iSuccess
Get ComFrom Of hoEmail To sTemp1
Showln sTemp1
Get ComSubject Of hoEmail To sTemp1
Showln sTemp1
Showln "----"
Move (i + 1) To i
Loop
End_Procedure