Sample code for 30+ languages & platforms
Python Requires Chilkat v11.0.0+

Preview POP3 messages and download a complete email

See more Email Object Examples

Read a short preview for each of up to five messages into an EmailBundle, print their subjects, and download the complete first entry into a separate Email. Configure a POP3 account that supports UIDL and TOP. All messages stay on the server.

Chilkat Python Downloads

Python
import sys
import chilkat2

success = False

# Assumes Chilkat is unlocked. See Global Unlock Sample.

mail = chilkat2.MailMan()
mail.MailHost = "pop.example.com"
mail.MailPort = 995
mail.PopSsl = True
mail.RequireSslCertVerify = True
mail.PopUsername = "MAIL_USER"
mail.PopPassword = "MAIL_PASSWORD"
previews = chilkat2.EmailBundle()
# Keep the messages and request minimal previews. Range indexes are zero-based and inclusive.
success = mail.FetchRange(True,True,0,0,4,previews)
if (success != True):
    print(mail.LastErrorText)
    sys.exit()

if (previews.MessageCount == 0):
    print("There are no messages to preview.")
    success = mail.Pop3EndSession()
    if (success != True):
        print(mail.LastErrorText)
        sys.exit()

    sys.exit()

header = chilkat2.Email()
i = 0
while (i < previews.MessageCount) :
    success = previews.EmailAt(i,header)
    if (success != True):
        print(previews.LastErrorText)
        sys.exit()

    print(header.Uidl + "  " + header.Subject)
    i = i + 1

# A UI could use its selected row here. FetchFull uses the preview's POP3 identity.
success = previews.EmailAt(0,header)
if (success != True):
    print(previews.LastErrorText)
    sys.exit()

email = chilkat2.Email()
success = mail.FetchFull(header,email)
if (success != True):
    print(mail.LastErrorText)
    sys.exit()

success = mail.Pop3EndSession()
if (success != True):
    print(mail.LastErrorText)
    sys.exit()

print("Full message: " + email.Subject + "  Files: " + str(email.NumAttachments))

success = email.SaveEml("pop-received.eml")
if (success != True):
    print(email.LastErrorText)
    sys.exit()

print("Saved pop-received.eml.")