Sample code for 30+ languages & platforms
Lianja

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 Lianja Downloads

Lianja
llSuccess = .F.

//  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).

loImap = createobject("CkImap")

loImap.Ssl = .T.
loImap.Port = 993

llSuccess = loImap.Connect("imap.example.com")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

llSuccess = loImap.Login("user@example.com","myPassword")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

llSuccess = loImap.SelectMailbox("Inbox")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

//  Find the messages to download (here, the unseen messages by UID).
loMsgSet = createobject("CkMessageSet")
llSuccess = loImap.QueryMbx("UNSEEN",.T.,loMsgSet)
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loMsgSet
    return
endif

//  Download the messages in the set (full bodies) into a bundle.
loBundle = createobject("CkEmailBundle")
llSuccess = loImap.FetchMsgSet(.F.,loMsgSet,loBundle)
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loMsgSet
    release loBundle
    return
endif

n = loBundle.MessageCount
? "Fetched " + str(n) + " messages."
loEmail = createobject("CkEmail")

for i = 0 to n - 1
    llSuccess = loBundle.EmailAt(i,loEmail)
    ? loEmail.Subject
next

llSuccess = loImap.Disconnect()
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loMsgSet
    release loBundle
    release loEmail
    return
endif



release loImap
release loMsgSet
release loBundle
release loEmail