Sample code for 30+ languages & platforms
Chilkat2-Python

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 Chilkat2-Python Downloads

Chilkat2-Python
import sys
import chilkat2

success = False

# 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.
mailman = chilkat2.MailMan()

# Set the POP3 server's hostname
mailman.MailHost = "pop.example.com"

# Set the POP3 login/password.
mailman.PopUsername = "***"
mailman.PopPassword = "***"

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

sbXml = chilkat2.StringBuilder()
htSeenUidls = chilkat2.Hashtable()
fac = chilkat2.FileAccess()
if (fac.FileExists(seenUidlsPath) == True):
    success = sbXml.LoadFile(seenUidlsPath,"utf-8")
    if (success == False):
        print(sbXml.LastErrorText)
        sys.exit()

    htSeenUidls.AddFromXmlSb(sbXml)

# Get the complete list of UIDLs on the mail server.
stUidls = chilkat2.StringTable()
success = mailman.FetchUidls(stUidls)
if (success == False):
    print(mailman.LastErrorText)
    sys.exit()

# Build a list of unseen UIDLs
stUnseenUidls = chilkat2.StringTable()

i = 0
count = stUidls.Count
while i < count :
    uidl = stUidls.StringAt(i)
    if (htSeenUidls.Contains(uidl) != True):
        stUnseenUidls.Append(uidl)

    i = i + 1

if (stUnseenUidls.Count == 0):
    print("No unseen emails!")
    sys.exit()

# Download the unseen emails, adding each UIDL to the "seen" hash table.
email = chilkat2.Email()

count = stUnseenUidls.Count
i = 0
while i < count :
    # Download the full email.
    uidl = stUnseenUidls.StringAt(i)
    success = mailman.FetchByUidl(uidl,False,0,email)
    if (success == False):
        print(mailman.LastErrorText)
        sys.exit()

    print(str(i))
    print("From: " + email.From)
    print("Subject: " + email.Subject)

    # Add this UIDL to the "seen" hash table.
    htSeenUidls.AddStr(uidl,"")

    i = i + 1

mailman.Pop3EndSession()

# Update the "seen" UIDLs file.
sbXml.Clear()
htSeenUidls.ToXmlSb(sbXml)
success = sbXml.WriteFile(seenUidlsPath,"utf-8",False)
if (success == False):
    print(sbXml.LastErrorText)
    sys.exit()

print("Success.")