Sample code for 30+ languages & platforms
Xbase++

Fetch a Single Message's MIME into a StringBuilder

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchSingleAsMimeSb method, which downloads one message's MIME into a StringBuilder. The first argument is the message id, the second (bUid) selects UID vs sequence number, and the third is the StringBuilder, which is cleared first. This example downloads a message by UID and prints the MIME length.

Background: Chilkat interprets the received MIME as UTF-8. Messages using Base64 or quoted-printable transfer encoding are safe because their encoded bytes are ASCII. Raw 8bit or binary content, or unencoded text in a charset such as ISO-8859-1 or Shift_JIS, can be misinterpreted — in those cases use FetchSingleBd to get the exact bytes.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL nWantUids
LOCAL oMsgSet
LOCAL nUid
LOCAL nUseUid
LOCAL oSbMime

nSuccess := 0

//  Demonstrates the Imap.FetchSingleAsMimeSb method, which downloads one message's MIME into a
//  StringBuilder.  The 1st argument is the message id, the 2nd (bUid) selects UID vs sequence
//  number, and the 3rd is the StringBuilder (cleared first).

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

//  Search for the message ids to download, returning UIDs.
nWantUids := 1
oMsgSet := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx("ALL", nWantUids, oMsgSet)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMsgSet:destroy()
    RETURN
ENDIF

IF (oMsgSet:Count > 0)
    nUid := oMsgSet:GetId(0)

    //  Download the message's MIME into a StringBuilder.  The id is a UID.
    nUseUid := 1
    oSbMime := CreateObject("Chilkat.StringBuilder")
    nSuccess := oImap:FetchSingleAsMimeSb(nUid, nUseUid, oSbMime)
    IF (nSuccess == 0)
        ? oImap:LastErrorText
        oImap:destroy()
        oMsgSet:destroy()
        oSbMime:destroy()
        RETURN
    ENDIF

    ? "MIME length: " + Str(oSbMime:Length) + " chars"
ENDIF

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

oImap:destroy()
oMsgSet:destroy()
oSbMime:destroy()