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

IMAP Download All Email One at a Time

Demonstrates how to download every email in an IMAP mailbox one at a time as a MIME string or as an email object. (The MIME contains the full contents of the email including all attachments.)

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL nBUid
LOCAL cMimeStr
LOCAL i
LOCAL n
LOCAL oEmail

nSuccess := 0

oImap := CreateObject("Chilkat.Imap")

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

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

//  Login
nSuccess := oImap:Login("myLogin", "myPassword")
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

//  Once the mailbox is selected, the NumMessages property
//  will contain the number of messages in the mailbox.
//  You may loop from 1 to NumMessages to
//  fetch each message by sequence number.

nBUid := 0

n := oImap:NumMessages
FOR i := 1 TO n

    //  Download the email by sequence number.
    cMimeStr := oImap:FetchSingleAsMime(i, nBUid)

    //  ... your application may process each MIME string...
NEXT

//  An alternative is to download each email in the form of an
//  email object, like this:
oEmail := CreateObject("Chilkat.Email")
FOR i := 1 TO n

    //  Download the email by sequence number.
    nSuccess := oImap:FetchEmail(0, i, nBUid, oEmail)

    //  ... your application may process the email object...

NEXT

//  Disconnect from the IMAP server.
nSuccess := oImap:Disconnect()

oImap:destroy()
oEmail:destroy()