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

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL nFetchUids
LOCAL oMessageSet
LOCAL oEmail
LOCAL i

nSuccess := 0

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

oImap := CreateObject("Chilkat.Imap")

//  Connect using TLS.
oImap:Ssl := 1
oImap:Port := 993
nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Authenticate
nSuccess := oImap:Login("email_account_login", "email_account_password")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Select an IMAP mailbox
nSuccess := oImap:SelectMailbox("Inbox")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

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

oMessageSet := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx("ALL", nFetchUids, oMessageSet)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMessageSet:destroy()
    RETURN
ENDIF

? "Number of messages = " + Str(oMessageSet:Count)

oEmail := CreateObject("Chilkat.Email")
i := 0
DO WHILE i < oMessageSet:Count
    nSuccess := oImap:FetchEmail(0, oMessageSet:GetId(i), nFetchUids, oEmail)
    IF (nSuccess == 0)
        ? oImap:LastErrorText
        oImap:destroy()
        oMessageSet:destroy()
        oEmail:destroy()
        RETURN
    ENDIF

    ? oEmail:From + "; " + oEmail:Subject

    i := i + 1
ENDDO

? "OK"

oImap:destroy()
oMessageSet:destroy()
oEmail:destroy()