Sample code for 30+ languages & platforms
Tcl

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

Tcl

load ./chilkat.dll

set success 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).

set imap [new_CkImap]

CkImap_put_Ssl $imap 1
CkImap_put_Port $imap 993

set success [CkImap_Connect $imap "imap.example.com"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

set success [CkImap_Login $imap "user@example.com" "myPassword"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

set success [CkImap_SelectMailbox $imap "Inbox"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

#  Search for the message ids to download, returning UIDs.
set wantUids 1
set msgSet [new_CkMessageSet]

set success [CkImap_QueryMbx $imap "ALL" $wantUids $msgSet]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkMessageSet $msgSet
    exit
}

if {[CkMessageSet_get_Count $msgSet] > 0} then {
    set uid [CkMessageSet_GetId $msgSet 0]

    #  Download the message's MIME into a StringBuilder.  The id is a UID.
    set useUid 1
    set sbMime [new_CkStringBuilder]

    set success [CkImap_FetchSingleAsMimeSb $imap $uid $useUid $sbMime]
    if {$success == 0} then {
        puts [CkImap_lastErrorText $imap]
        delete_CkImap $imap
        delete_CkMessageSet $msgSet
        delete_CkStringBuilder $sbMime
        exit
    }

    puts "MIME length: [CkStringBuilder_get_Length $sbMime] chars"
}

set success [CkImap_Disconnect $imap]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkMessageSet $msgSet
    delete_CkStringBuilder $sbMime
    exit
}


delete_CkImap $imap
delete_CkMessageSet $msgSet
delete_CkStringBuilder $sbMime