Sample code for 30+ languages & platforms
Tcl

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

Tcl

load ./chilkat.dll

set 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.
set mailman [new_CkMailMan]

# Set the POP3 server's hostname
CkMailMan_put_MailHost $mailman "pop.example.com"

# Set the POP3 login/password.
CkMailMan_put_PopUsername $mailman "***"
CkMailMan_put_PopPassword $mailman "***"

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

set sbXml [new_CkStringBuilder]

set htSeenUidls [new_CkHashtable]

set fac [new_CkFileAccess]

if {[CkFileAccess_FileExists $fac $seenUidlsPath] == 1} then {
    set success [CkStringBuilder_LoadFile $sbXml $seenUidlsPath "utf-8"]
    if {$success == 0} then {
        puts [CkStringBuilder_lastErrorText $sbXml]
        delete_CkMailMan $mailman
        delete_CkStringBuilder $sbXml
        delete_CkHashtable $htSeenUidls
        delete_CkFileAccess $fac
        exit
    }

    CkHashtable_AddFromXmlSb $htSeenUidls $sbXml
}

# Get the complete list of UIDLs on the mail server.
set stUidls [new_CkStringTable]

set success [CkMailMan_FetchUidls $mailman $stUidls]
if {$success == 0} then {
    puts [CkMailMan_lastErrorText $mailman]
    delete_CkMailMan $mailman
    delete_CkStringBuilder $sbXml
    delete_CkHashtable $htSeenUidls
    delete_CkFileAccess $fac
    delete_CkStringTable $stUidls
    exit
}

# Build a list of unseen UIDLs
set stUnseenUidls [new_CkStringTable]

set i 0
set count [CkStringTable_get_Count $stUidls]
while {$i < $count} {
    set uidl [CkStringTable_stringAt $stUidls $i]
    if {[CkHashtable_Contains $htSeenUidls $uidl] != 1} then {
        CkStringTable_Append $stUnseenUidls $uidl
    }

    set i [expr $i + 1]
}

if {[CkStringTable_get_Count $stUnseenUidls] == 0} then {
    puts "No unseen emails!"
    delete_CkMailMan $mailman
    delete_CkStringBuilder $sbXml
    delete_CkHashtable $htSeenUidls
    delete_CkFileAccess $fac
    delete_CkStringTable $stUidls
    delete_CkStringTable $stUnseenUidls
    exit
}

# Download the unseen emails, adding each UIDL to the "seen" hash table.
set email [new_CkEmail]

set count [CkStringTable_get_Count $stUnseenUidls]
set i 0
while {$i < $count} {
    # Download the full email.
    set uidl [CkStringTable_stringAt $stUnseenUidls $i]
    set success [CkMailMan_FetchByUidl $mailman $uidl 0 0 $email]
    if {$success == 0} then {
        puts [CkMailMan_lastErrorText $mailman]
        delete_CkMailMan $mailman
        delete_CkStringBuilder $sbXml
        delete_CkHashtable $htSeenUidls
        delete_CkFileAccess $fac
        delete_CkStringTable $stUidls
        delete_CkStringTable $stUnseenUidls
        delete_CkEmail $email
        exit
    }

    puts "$i"
    puts "From: [CkEmail_from $email]"
    puts "Subject: [CkEmail_subject $email]"

    # Add this UIDL to the "seen" hash table.
    CkHashtable_AddStr $htSeenUidls $uidl ""

    set i [expr $i + 1]
}

CkMailMan_Pop3EndSession $mailman

# Update the "seen" UIDLs file.
CkStringBuilder_Clear $sbXml
CkHashtable_ToXmlSb $htSeenUidls $sbXml
set success [CkStringBuilder_WriteFile $sbXml $seenUidlsPath "utf-8" 0]
if {$success == 0} then {
    puts [CkStringBuilder_lastErrorText $sbXml]
    delete_CkMailMan $mailman
    delete_CkStringBuilder $sbXml
    delete_CkHashtable $htSeenUidls
    delete_CkFileAccess $fac
    delete_CkStringTable $stUidls
    delete_CkStringTable $stUnseenUidls
    delete_CkEmail $email
    exit
}

puts "Success."

delete_CkMailMan $mailman
delete_CkStringBuilder $sbXml
delete_CkHashtable $htSeenUidls
delete_CkFileAccess $fac
delete_CkStringTable $stUidls
delete_CkStringTable $stUnseenUidls
delete_CkEmail $email