Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loMailman
LOCAL loStUidls
LOCAL lnStartIdx
LOCAL n
LOCAL lnCount
LOCAL loStUidls2
LOCAL lnEndIdx
LOCAL i
LOCAL loBundle
LOCAL lnHeadersOnly
LOCAL lnNumBodyLines
LOCAL loEmail

lnSuccess = 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.
loMailman = CreateObject('Chilkat.MailMan')

* Set the POP3 server's hostname
loMailman.MailHost = "pop.example.com"

* Set the POP3 login/password.
loMailman.PopUsername = "bob@example.com"
loMailman.PopPassword = "****"

* Get the complete list of UIDLs
loStUidls = CreateObject('Chilkat.StringTable')
lnSuccess = loMailman.FetchUidls(loStUidls)
IF (lnSuccess = 0) THEN
    ? loMailman.LastErrorText
    RELEASE loMailman
    RELEASE loStUidls
    CANCEL
ENDIF

* Get the 10 most recent UIDLs
* The 1st email is the oldest, the last email is the newest (usually)
lnStartIdx = 0
n = loStUidls.Count
IF (n = 0) THEN
    ? "No email in the inbox."
    RELEASE loMailman
    RELEASE loStUidls
    CANCEL
ENDIF

lnCount = 10
IF (n > 10) THEN
    lnStartIdx = n - 10
ELSE
    lnStartIdx = 0
ENDIF

loStUidls2 = CreateObject('Chilkat.StringTable')

lnEndIdx = n - 1

FOR i = lnStartIdx TO lnEndIdx
    loStUidls2.Append(loStUidls.StringAt(i))
NEXT

* Download in full the 10 most recent emails:
loBundle = CreateObject('Chilkat.EmailBundle')
lnHeadersOnly = 0
* numBodyLines is ignored when fetching full emails.
lnNumBodyLines = 0
lnSuccess = loMailman.FetchUidlSet(loStUidls2,lnHeadersOnly,lnNumBodyLines,loBundle)
IF (lnSuccess = 0) THEN
    ? loMailman.LastErrorText
    RELEASE loMailman
    RELEASE loStUidls
    RELEASE loStUidls2
    RELEASE loBundle
    CANCEL
ENDIF

loEmail = CreateObject('Chilkat.Email')
i = 0
DO WHILE i < loBundle.MessageCount
    loBundle.EmailAt(i,loEmail)
    ? loEmail.From
    ? loEmail.Subject
    ? "----"

    i = i + 1
ENDDO

RELEASE loMailman
RELEASE loStUidls
RELEASE loStUidls2
RELEASE loBundle
RELEASE loEmail