Sample code for 30+ languages & platforms
PowerBuilder

Fetch a Text IMAP Attachment as a String

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchAttachmentString method, which returns one text attachment decoded to a string. The first argument is the Email, the second is the zero-based attachment index, and the third is the charset used to decode the bytes. This example returns the first attachment as a UTF-8 string.

Background: This is the convenient form when you simply want a small text attachment's content inline as a string. Only use it for attachments known to be text; decoding binary content through a charset can alter or corrupt the bytes. For binary data use FetchAttachmentBd, and for large text a StringBuilder (via FetchAttachmentSb) avoids an extra copy.

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
string ls_AttachText

li_Success = 0

//  Demonstrates the Imap.FetchAttachmentString method, which downloads one text attachment and
//  returns it decoded to a string.  The 1st argument is the Email, the 2nd is the zero-based
//  attachment index, and the 3rd is the charset used to decode the bytes.
//  
//  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
    //  FetchAttachmentString returns a string, so assign it to a string variable.
    ls_AttachText = loo_Imap.FetchAttachmentString(loo_Email,0,"utf-8")
    if loo_Imap.LastMethodSuccess = 0 then
        Write-Debug loo_Imap.LastErrorText
        destroy loo_Imap
        destroy loo_Email
        return
    end if

    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
    return
end if



destroy loo_Imap
destroy loo_Email