Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Mailman
oleobject loo_StUidls
integer li_StartIdx
integer n
integer li_Count
oleobject loo_StUidls2
integer li_EndIdx
integer i
oleobject loo_Bundle
integer li_HeadersOnly
integer li_NumBodyLines
oleobject loo_Email

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 = "bob@example.com"
loo_Mailman.PopPassword = "****"

// Get the complete list of UIDLs
loo_StUidls = create oleobject
li_rc = loo_StUidls.ConnectToNewObject("Chilkat.StringTable")

li_Success = loo_Mailman.FetchUidls(loo_StUidls)
if li_Success = 0 then
    Write-Debug loo_Mailman.LastErrorText
    destroy loo_Mailman
    destroy loo_StUidls
    return
end if

// Get the 10 most recent UIDLs
// The 1st email is the oldest, the last email is the newest (usually)
li_StartIdx = 0
n = loo_StUidls.Count
if n = 0 then
    Write-Debug "No email in the inbox."
    destroy loo_Mailman
    destroy loo_StUidls
    return
end if

li_Count = 10
if n > 10 then
    li_StartIdx = n - 10
else
    li_StartIdx = 0
end if

loo_StUidls2 = create oleobject
li_rc = loo_StUidls2.ConnectToNewObject("Chilkat.StringTable")

li_EndIdx = n - 1

for i = li_StartIdx to li_EndIdx
    loo_StUidls2.Append(loo_StUidls.StringAt(i))
next

// Download in full the 10 most recent emails:
loo_Bundle = create oleobject
li_rc = loo_Bundle.ConnectToNewObject("Chilkat.EmailBundle")

li_HeadersOnly = 0
// numBodyLines is ignored when fetching full emails.
li_NumBodyLines = 0
li_Success = loo_Mailman.FetchUidlSet(loo_StUidls2,li_HeadersOnly,li_NumBodyLines,loo_Bundle)
if li_Success = 0 then
    Write-Debug loo_Mailman.LastErrorText
    destroy loo_Mailman
    destroy loo_StUidls
    destroy loo_StUidls2
    destroy loo_Bundle
    return
end if

loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

i = 0
do while i < loo_Bundle.MessageCount
    loo_Bundle.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_StUidls
destroy loo_StUidls2
destroy loo_Bundle
destroy loo_Email