Sample code for 30+ languages & platforms
CkPython 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 CkPython Downloads

CkPython
import sys
import chilkat

success = False

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

mail = chilkat.CkMailMan()
mail.put_MailHost("pop.example.com")
mail.put_MailPort(995)
mail.put_PopSsl(True)
mail.put_RequireSslCertVerify(True)
mail.put_PopUsername("MAIL_USER")
mail.put_PopPassword("MAIL_PASSWORD")
previews = chilkat.CkEmailBundle()
#  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.get_MessageCount() == 0):
    print("There are no messages to preview.")
    success = mail.Pop3EndSession()
    if (success != True):
        print(mail.lastErrorText())
        sys.exit()

    sys.exit()

header = chilkat.CkEmail()
i = 0
while (i < previews.get_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 = chilkat.CkEmail()
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.get_NumAttachments()))

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

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