Sample code for 30+ languages & platforms
Xbase++

Fetch a MessageSet into an EmailBundle

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchMsgSet method, which downloads the messages identified by a MessageSet and appends them to an EmailBundle. The arguments are headersOnly, the MessageSet, and the EmailBundle (which is not cleared). This example searches for unseen messages and downloads the whole set.

Background: This is the natural pairing with QueryMbx: search to get a MessageSet of matching identifiers, then download exactly those messages in one call. Fetching a set is far more efficient than looping one message at a time, and it returns an EmailBundle you iterate with MessageCount and EmailAt. Pass headersOnly = true to pull just headers for a fast preview list.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL oMsgSet
LOCAL oBundle
LOCAL n
LOCAL oEmail
LOCAL i

nSuccess := 0

//  Demonstrates the Imap.FetchMsgSet method, which downloads the messages identified by a
//  MessageSet and appends them to an EmailBundle.  The 1st argument is headersOnly, the 2nd
//  is the MessageSet, and the 3rd is the EmailBundle (which is not cleared).

oImap := CreateObject("Chilkat.Imap")

oImap:Ssl := 1
oImap:Port := 993

nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

nSuccess := oImap:Login("user@example.com", "myPassword")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

nSuccess := oImap:SelectMailbox("Inbox")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Find the messages to download (here, the unseen messages by UID).
oMsgSet := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx("UNSEEN", 1, oMsgSet)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMsgSet:destroy()
    RETURN
ENDIF

//  Download the messages in the set (full bodies) into a bundle.
oBundle := CreateObject("Chilkat.EmailBundle")
nSuccess := oImap:FetchMsgSet(0, oMsgSet, oBundle)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMsgSet:destroy()
    oBundle:destroy()
    RETURN
ENDIF

n := oBundle:MessageCount
? "Fetched " + Str(n) + " messages."
oEmail := CreateObject("Chilkat.Email")

FOR i := 0 TO n - 1
    nSuccess := oBundle:EmailAt(i, oEmail)
    ? oEmail:Subject
NEXT

nSuccess := oImap:Disconnect()
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMsgSet:destroy()
    oBundle:destroy()
    oEmail:destroy()
    RETURN
ENDIF

oImap:destroy()
oMsgSet:destroy()
oBundle:destroy()
oEmail:destroy()