Sample code for 30+ languages & platforms
Visual FoxPro

How to Download Messages in MessageSet One at a Time

See more IMAP Examples

If a message set contains a huge number of emails, it's NOT a good idea to try to download all at once into an email bundle using a method such as FetchBundle. It's better to iterate over the messages in the set to download one by one.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loImap
LOCAL lnFetchUids
LOCAL loMessageSet
LOCAL loEmail
LOCAL i

lnSuccess = 0

* This example requires the Chilkat API to have been previously unlocked.
* See Global Unlock Sample for sample code.

loImap = CreateObject('Chilkat.Imap')

* Connect using TLS.
loImap.Ssl = 1
loImap.Port = 993
lnSuccess = loImap.Connect("imap.example.com")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

* Authenticate
lnSuccess = loImap.Login("email_account_login","email_account_password")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

* Select an IMAP mailbox
lnSuccess = loImap.SelectMailbox("Inbox")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

* Search for messages and return a set of matching messages.
* (This example will simply search for ALL messages.)
lnFetchUids = 1

loMessageSet = CreateObject('Chilkat.MessageSet')
lnSuccess = loImap.QueryMbx("ALL",lnFetchUids,loMessageSet)
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loMessageSet
    CANCEL
ENDIF

? "Number of messages = " + STR(loMessageSet.Count)

loEmail = CreateObject('Chilkat.Email')
i = 0
DO WHILE i < loMessageSet.Count
    lnSuccess = loImap.FetchEmail(0,loMessageSet.GetId(i),lnFetchUids,loEmail)
    IF (lnSuccess = 0) THEN
        ? loImap.LastErrorText
        RELEASE loImap
        RELEASE loMessageSet
        RELEASE loEmail
        CANCEL
    ENDIF

    ? loEmail.From + "; " + loEmail.Subject

    i = i + 1
ENDDO

? "OK"

RELEASE loImap
RELEASE loMessageSet
RELEASE loEmail