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

Visual FoxPro
LOCAL lnSuccess
LOCAL loMail
LOCAL loPreviews
LOCAL loHeader
LOCAL i
LOCAL loEmail

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.

loMail = CreateObject('Chilkat.MailMan')
loMail.MailHost = "pop.example.com"
loMail.MailPort = 995
loMail.PopSsl = 1
loMail.RequireSslCertVerify = 1
loMail.PopUsername = "MAIL_USER"
loMail.PopPassword = "MAIL_PASSWORD"
loPreviews = CreateObject('Chilkat.EmailBundle')
*  Keep the messages and request minimal previews. Range indexes are zero-based and inclusive.
lnSuccess = loMail.FetchRange(1,1,0,0,4,loPreviews)
IF (lnSuccess <> 1) THEN
    ? loMail.LastErrorText
    RELEASE loMail
    RELEASE loPreviews
    CANCEL
ENDIF

IF (loPreviews.MessageCount = 0) THEN
    ? "There are no messages to preview."
    lnSuccess = loMail.Pop3EndSession()
    IF (lnSuccess <> 1) THEN
        ? loMail.LastErrorText
        RELEASE loMail
        RELEASE loPreviews
        CANCEL
    ENDIF

    RELEASE loMail
    RELEASE loPreviews
    CANCEL
ENDIF

loHeader = CreateObject('Chilkat.Email')
i = 0
DO WHILE (i < loPreviews.MessageCount)
    lnSuccess = loPreviews.EmailAt(i,loHeader)
    IF (lnSuccess <> 1) THEN
        ? loPreviews.LastErrorText
        RELEASE loMail
        RELEASE loPreviews
        RELEASE loHeader
        CANCEL
    ENDIF

    ? loHeader.Uidl + "  " + loHeader.Subject
    i = i + 1
ENDDO
*  A UI could use its selected row here. FetchFull uses the preview's POP3 identity.
lnSuccess = loPreviews.EmailAt(0,loHeader)
IF (lnSuccess <> 1) THEN
    ? loPreviews.LastErrorText
    RELEASE loMail
    RELEASE loPreviews
    RELEASE loHeader
    CANCEL
ENDIF

loEmail = CreateObject('Chilkat.Email')
lnSuccess = loMail.FetchFull(loHeader,loEmail)
IF (lnSuccess <> 1) THEN
    ? loMail.LastErrorText
    RELEASE loMail
    RELEASE loPreviews
    RELEASE loHeader
    RELEASE loEmail
    CANCEL
ENDIF

lnSuccess = loMail.Pop3EndSession()
IF (lnSuccess <> 1) THEN
    ? loMail.LastErrorText
    RELEASE loMail
    RELEASE loPreviews
    RELEASE loHeader
    RELEASE loEmail
    CANCEL
ENDIF

? "Full message: " + loEmail.Subject + "  Files: " + STR(loEmail.NumAttachments)

lnSuccess = loEmail.SaveEml("pop-received.eml")
IF (lnSuccess <> 1) THEN
    ? loEmail.LastErrorText
    RELEASE loMail
    RELEASE loPreviews
    RELEASE loHeader
    RELEASE loEmail
    CANCEL
ENDIF

? "Saved pop-received.eml."

RELEASE loMail
RELEASE loPreviews
RELEASE loHeader
RELEASE loEmail