Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap
integer li_WantUids
oleobject loo_MsgSet
integer li_Uid
integer li_UseUid
oleobject loo_SbMime

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

loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")
if li_rc < 0 then
    destroy loo_Imap
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Imap.Ssl = 1
loo_Imap.Port = 993

li_Success = loo_Imap.Connect("imap.example.com")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.Login("user@example.com","myPassword")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.SelectMailbox("Inbox")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

//  Search for the message ids to download, returning UIDs.
li_WantUids = 1
loo_MsgSet = create oleobject
li_rc = loo_MsgSet.ConnectToNewObject("Chilkat.MessageSet")

li_Success = loo_Imap.QueryMbx("ALL",li_WantUids,loo_MsgSet)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_MsgSet
    return
end if

if loo_MsgSet.Count > 0 then
    li_Uid = loo_MsgSet.GetId(0)

    //  Download the message's MIME into a StringBuilder.  The id is a UID.
    li_UseUid = 1
    loo_SbMime = create oleobject
    li_rc = loo_SbMime.ConnectToNewObject("Chilkat.StringBuilder")

    li_Success = loo_Imap.FetchSingleAsMimeSb(li_Uid,li_UseUid,loo_SbMime)
    if li_Success = 0 then
        Write-Debug loo_Imap.LastErrorText
        destroy loo_Imap
        destroy loo_MsgSet
        destroy loo_SbMime
        return
    end if

    Write-Debug "MIME length: " + string(loo_SbMime.Length) + " chars"
end if

li_Success = loo_Imap.Disconnect()
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_MsgSet
    destroy loo_SbMime
    return
end if



destroy loo_Imap
destroy loo_MsgSet
destroy loo_SbMime