Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkImap.pb"
IncludeFile "CkMessageSet.pb"

Procedure ChilkatExample()

    success.i = 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).

    imap.i = CkImap::ckCreate()
    If imap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)

    success = CkImap::ckConnect(imap,"imap.example.com")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckLogin(imap,"user@example.com","myPassword")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckSelectMailbox(imap,"Inbox")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ;  Search for the message ids to download, returning UIDs.
    wantUids.i = 1
    msgSet.i = CkMessageSet::ckCreate()
    If msgSet.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckQueryMbx(imap,"ALL",wantUids,msgSet)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(msgSet)
        ProcedureReturn
    EndIf

    If CkMessageSet::ckCount(msgSet) > 0
        uid.i = CkMessageSet::ckGetId(msgSet,0)

        ;  Download the message's MIME into a StringBuilder.  The id is a UID.
        useUid.i = 1
        sbMime.i = CkStringBuilder::ckCreate()
        If sbMime.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        success = CkImap::ckFetchSingleAsMimeSb(imap,uid,useUid,sbMime)
        If success = 0
            Debug CkImap::ckLastErrorText(imap)
            CkImap::ckDispose(imap)
            CkMessageSet::ckDispose(msgSet)
            CkStringBuilder::ckDispose(sbMime)
            ProcedureReturn
        EndIf

        Debug "MIME length: " + Str(CkStringBuilder::ckLength(sbMime)) + " chars"
    EndIf

    success = CkImap::ckDisconnect(imap)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(msgSet)
        CkStringBuilder::ckDispose(sbMime)
        ProcedureReturn
    EndIf



    CkImap::ckDispose(imap)
    CkMessageSet::ckDispose(msgSet)
    CkStringBuilder::ckDispose(sbMime)


    ProcedureReturn
EndProcedure