Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    Boolean iWantUids
    Variant vMsgSet
    Handle hoMsgSet
    UInteger iUid
    Boolean iUseUid
    Variant vSbMime
    Handle hoSbMime
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatImap)) To hoImap
    If (Not(IsComObjectCreated(hoImap))) Begin
        Send CreateComObject of hoImap
    End

    Set ComSsl Of hoImap To True
    Set ComPort Of hoImap To 993

    Get ComConnect Of hoImap "imap.example.com" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComLogin Of hoImap "user@example.com" "myPassword" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSelectMailbox Of hoImap "Inbox" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Search for the message ids to download, returning UIDs.
    Move True To iWantUids
    Get Create (RefClass(cComChilkatMessageSet)) To hoMsgSet
    If (Not(IsComObjectCreated(hoMsgSet))) Begin
        Send CreateComObject of hoMsgSet
    End
    Get pvComObject of hoMsgSet to vMsgSet
    Get ComQueryMbx Of hoImap "ALL" iWantUids vMsgSet To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComCount Of hoMsgSet To iTemp1
    If (iTemp1 > 0) Begin
        Get ComGetId Of hoMsgSet 0 To iUid

        //  Download the message's MIME into a StringBuilder.  The id is a UID.
        Move True To iUseUid
        Get Create (RefClass(cComChilkatStringBuilder)) To hoSbMime
        If (Not(IsComObjectCreated(hoSbMime))) Begin
            Send CreateComObject of hoSbMime
        End
        Get pvComObject of hoSbMime to vSbMime
        Get ComFetchSingleAsMimeSb Of hoImap iUid iUseUid vSbMime To iSuccess
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoImap To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Get ComLength Of hoSbMime To iTemp1
        Showln "MIME length: " iTemp1 " chars"
    End

    Get ComDisconnect Of hoImap To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure