Sample code for 30+ languages & platforms
PowerBuilder

Fetch a Text IMAP Attachment into a StringBuilder

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchAttachmentSb method, which decodes one text attachment into a StringBuilder. The first argument is the Email, the second is the zero-based attachment index, the third is the charset used to decode the bytes, and the fourth is the StringBuilder, which is cleared first. This example decodes the first attachment as UTF-8.

Background: For text attachments — a CSV, a .txt, an XML file — decoding straight to characters saves a manual charset step. The charset argument names the encoding of the attachment bytes (for example utf-8 or iso-8859-1). For binary attachments, use FetchAttachmentBd instead, since forcing binary through a text decoder can corrupt it.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap
integer li_HeadersOnly
integer li_UseUid
integer li_SeqNum
oleobject loo_Email
integer li_NumAttach
oleobject loo_Sb
string ls_AttachText

li_Success = 0

//  Demonstrates the Imap.FetchAttachmentSb method, which downloads one text attachment and
//  decodes it into a StringBuilder.  The 1st argument is the Email, the 2nd is the zero-based
//  attachment index, the 3rd is the charset used to decode, and the 4th is the StringBuilder
//  (cleared first).
//  
//  The message is fetched headers-only, then the attachment is downloaded on demand.

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

//  Fetch only the message headers.  Attachment bodies are NOT downloaded, but the ckx-imap-*
//  metadata describing the attachments is present, so the attachment info methods still work.
li_HeadersOnly = 1
li_UseUid = 0
li_SeqNum = 1
loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

li_Success = loo_Imap.FetchEmail(li_HeadersOnly,li_SeqNum,li_UseUid,loo_Email)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Email
    return
end if

li_NumAttach = loo_Imap.GetMailNumAttach(loo_Email)
if li_NumAttach > 0 then
    //  Download the first (text) attachment and decode it as UTF-8.  Use this only for text.
    loo_Sb = create oleobject
    li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder")

    li_Success = loo_Imap.FetchAttachmentSb(loo_Email,0,"utf-8",loo_Sb)
    if li_Success = 0 then
        Write-Debug loo_Imap.LastErrorText
        destroy loo_Imap
        destroy loo_Email
        destroy loo_Sb
        return
    end if

    ls_AttachText = loo_Sb.GetAsString()
    Write-Debug ls_AttachText
end if

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



destroy loo_Imap
destroy loo_Email
destroy loo_Sb