Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.0.0+

Find and save an attachment and list inline images

See more Email Object Examples

Find an attachment by filename and media type, then save its decoded bytes to a path you choose. Enumerate inline resources separately to read their Content-IDs.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Email
oleobject loo_Filename
oleobject loo_MediaType
long li_Found
long i
oleobject loo_Data

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")
if li_rc < 0 then
    destroy loo_Email
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Email.LoadEml("with-attachments.eml")
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    return
end if

loo_Filename = create oleobject
li_rc = loo_Filename.ConnectToNewObject("Chilkat.StringBuilder")

loo_MediaType = create oleobject
li_rc = loo_MediaType.ConnectToNewObject("Chilkat.StringBuilder")

li_Found = 0
i = 0
do while (i < loo_Email.NumAttachments)
    loo_Filename.SetString(loo_Email.GetAttachmentFilename(i))
    loo_MediaType.SetString(loo_Email.GetAttachmentContentType(i))
    if (loo_Filename.ContentsEqual("agenda.csv",0) = 1) AND (loo_MediaType.ContentsEqual("text/csv",0) = 1) AND (li_Found = 0) then
        loo_Data = create oleobject
        li_rc = loo_Data.ConnectToNewObject("Chilkat.BinData")

        li_Success = loo_Email.GetAttachmentBd(i,loo_Data)
        if li_Success <> 1 then
            Write-Debug loo_Email.LastErrorText
            destroy loo_Email
            destroy loo_Filename
            destroy loo_MediaType
            destroy loo_Data
            return
        end if

        li_Success = loo_Data.WriteFile("received-agenda.csv")
        if li_Success <> 1 then
            Write-Debug "Failed: data.WriteFile."
            destroy loo_Email
            destroy loo_Filename
            destroy loo_MediaType
            destroy loo_Data
            return
        end if

        Write-Debug "Saved attachment: " + string(loo_Data.NumBytes) + " bytes."
        li_Found = 1
    end if

    i = i + 1
loop
if li_Found <> 1 then
    Write-Debug "No matching attachment."
end if

i = 0
do while (i < loo_Email.NumRelatedItems)
    Write-Debug "Related: " + loo_Email.GetRelatedContentType(i) + "  Content-ID: " + loo_Email.GetRelatedContentID(i)
    i = i + 1
loop


destroy loo_Email
destroy loo_Filename
destroy loo_MediaType
destroy loo_Data