Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loImap
LOCAL loMsgSet
LOCAL loBundle
LOCAL n
LOCAL loEmail
LOCAL i

lnSuccess = 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).

loImap = CreateObject('Chilkat.Imap')

loImap.Ssl = 1
loImap.Port = 993

lnSuccess = loImap.Connect("imap.example.com")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

lnSuccess = loImap.Login("user@example.com","myPassword")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

lnSuccess = loImap.SelectMailbox("Inbox")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

*  Find the messages to download (here, the unseen messages by UID).
loMsgSet = CreateObject('Chilkat.MessageSet')
lnSuccess = loImap.QueryMbx("UNSEEN",1,loMsgSet)
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loMsgSet
    CANCEL
ENDIF

*  Download the messages in the set (full bodies) into a bundle.
loBundle = CreateObject('Chilkat.EmailBundle')
lnSuccess = loImap.FetchMsgSet(0,loMsgSet,loBundle)
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loMsgSet
    RELEASE loBundle
    CANCEL
ENDIF

n = loBundle.MessageCount
? "Fetched " + STR(n) + " messages."
loEmail = CreateObject('Chilkat.Email')

FOR i = 0 TO n - 1
    lnSuccess = loBundle.EmailAt(i,loEmail)
    ? loEmail.Subject
NEXT

lnSuccess = loImap.Disconnect()
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loMsgSet
    RELEASE loBundle
    RELEASE loEmail
    CANCEL
ENDIF

RELEASE loImap
RELEASE loMsgSet
RELEASE loBundle
RELEASE loEmail