Sample code for 30+ languages & platforms
Lianja

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

Lianja
llSuccess = .F.

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

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

//  Search for the message ids to download, returning UIDs.
llWantUids = .T.
loMsgSet = createobject("CkMessageSet")
llSuccess = loImap.QueryMbx("ALL",llWantUids,loMsgSet)
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loMsgSet
    return
endif

if (loMsgSet.Count > 0) then
    lnUid = loMsgSet.GetId(0)

    //  Download the message's MIME into a StringBuilder.  The id is a UID.
    llUseUid = .T.
    loSbMime = createobject("CkStringBuilder")
    llSuccess = loImap.FetchSingleAsMimeSb(lnUid,llUseUid,loSbMime)
    if (llSuccess = .F.) then
        ? loImap.LastErrorText
        release loImap
        release loMsgSet
        release loSbMime
        return
    endif

    ? "MIME length: " + str(loSbMime.Length) + " chars"
endif

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



release loImap
release loMsgSet
release loSbMime