Sample code for 30+ languages & platforms
PowerBuilder

Reading Unread POP3 Email

The POP3 protocol cannot determine which emails are "unread," and pure POP3 servers do not store this information. Servers like Exchange Server, offering both POP3 and IMAP interfaces, do contain read/unread data, but it's only accessible through IMAP. Email clients like Outlook and Thunderbird store read/unread statuses on the client side. The example demonstrates using UIDLs to track and manage "unread" emails.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Mailman
string ls_SeenUidlsPath
oleobject loo_SbXml
oleobject loo_HtSeenUidls
oleobject loo_Fac
oleobject loo_StUidls
oleobject loo_StUnseenUidls
string ls_Uidl
integer i
integer li_Count
oleobject loo_Email

li_Success = 0

// This example requires 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 = "***"
loo_Mailman.PopPassword = "***"

// Keep a records of already-seen UIDLs in hash table serialized to an XML file.
ls_SeenUidlsPath = "c:/temp/seenUidls.xml"

loo_SbXml = create oleobject
li_rc = loo_SbXml.ConnectToNewObject("Chilkat.StringBuilder")

loo_HtSeenUidls = create oleobject
li_rc = loo_HtSeenUidls.ConnectToNewObject("Chilkat.Hashtable")

loo_Fac = create oleobject
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")

if loo_Fac.FileExists(ls_SeenUidlsPath) = 1 then
    li_Success = loo_SbXml.LoadFile(ls_SeenUidlsPath,"utf-8")
    if li_Success = 0 then
        Write-Debug loo_SbXml.LastErrorText
        destroy loo_Mailman
        destroy loo_SbXml
        destroy loo_HtSeenUidls
        destroy loo_Fac
        return
    end if

    loo_HtSeenUidls.AddFromXmlSb(loo_SbXml)
end if

// Get the complete list of UIDLs on the mail server.
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_SbXml
    destroy loo_HtSeenUidls
    destroy loo_Fac
    destroy loo_StUidls
    return
end if

// Build a list of unseen UIDLs
loo_StUnseenUidls = create oleobject
li_rc = loo_StUnseenUidls.ConnectToNewObject("Chilkat.StringTable")

i = 0
li_Count = loo_StUidls.Count
do while i < li_Count
    ls_Uidl = loo_StUidls.StringAt(i)
    if loo_HtSeenUidls.Contains(ls_Uidl) <> 1 then
        loo_StUnseenUidls.Append(ls_Uidl)
    end if

    i = i + 1
loop

if loo_StUnseenUidls.Count = 0 then
    Write-Debug "No unseen emails!"
    destroy loo_Mailman
    destroy loo_SbXml
    destroy loo_HtSeenUidls
    destroy loo_Fac
    destroy loo_StUidls
    destroy loo_StUnseenUidls
    return
end if

// Download the unseen emails, adding each UIDL to the "seen" hash table.
loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

li_Count = loo_StUnseenUidls.Count
i = 0
do while i < li_Count
    // Download the full email.
    ls_Uidl = loo_StUnseenUidls.StringAt(i)
    li_Success = loo_Mailman.FetchByUidl(ls_Uidl,0,0,loo_Email)
    if li_Success = 0 then
        Write-Debug loo_Mailman.LastErrorText
        destroy loo_Mailman
        destroy loo_SbXml
        destroy loo_HtSeenUidls
        destroy loo_Fac
        destroy loo_StUidls
        destroy loo_StUnseenUidls
        destroy loo_Email
        return
    end if

    Write-Debug string(i)
    Write-Debug "From: " + loo_Email.From
    Write-Debug "Subject: " + loo_Email.Subject

    // Add this UIDL to the "seen" hash table.
    loo_HtSeenUidls.AddStr(ls_Uidl,"")

    i = i + 1
loop

loo_Mailman.Pop3EndSession()

// Update the "seen" UIDLs file.
loo_SbXml.Clear()
loo_HtSeenUidls.ToXmlSb(loo_SbXml)
li_Success = loo_SbXml.WriteFile(ls_SeenUidlsPath,"utf-8",0)
if li_Success = 0 then
    Write-Debug loo_SbXml.LastErrorText
    destroy loo_Mailman
    destroy loo_SbXml
    destroy loo_HtSeenUidls
    destroy loo_Fac
    destroy loo_StUidls
    destroy loo_StUnseenUidls
    destroy loo_Email
    return
end if

Write-Debug "Success."


destroy loo_Mailman
destroy loo_SbXml
destroy loo_HtSeenUidls
destroy loo_Fac
destroy loo_StUidls
destroy loo_StUnseenUidls
destroy loo_Email