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

Download MIME Source of Emails in IMAP Mailbox

Demonstrates how to download the MIME source for emails on an IMAP server.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL nNumMessages
LOCAL oSbMime
LOCAL nSeqNum

nSuccess := 0

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

oImap := CreateObject("Chilkat.Imap")

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

//  The NumMessages property contains the number of messages in the selected mailbox.
nNumMessages := oImap:NumMessages
IF (nNumMessages == 0)
    ? "No messages exist in the Inbox."
    oImap:destroy()
    RETURN
ENDIF

oSbMime := CreateObject("Chilkat.StringBuilder")

FOR nSeqNum := 1 TO nNumMessages
    oSbMime:Clear()
    nSuccess := oImap:FetchSingleAsMimeSb(nSeqNum, 0, oSbMime)
    IF (nSuccess == 0)
        ? oImap:LastErrorText
        oImap:destroy()
        oSbMime:destroy()
        RETURN
    ENDIF

    //  The MIME source of the downloaded email is contained in sbMime.
NEXT

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

oImap:destroy()
oSbMime:destroy()