Sample code for 30+ languages & platforms
PowerBuilder

Get an Attachment's Bytes into a BinData

See more Email Object Examples

Demonstrates the Chilkat Email.GetAttachmentBd method, which copies an attachment's binary data into a BinData object. The first attachment is at index 0. This example adds an attachment and copies its bytes into a BinData, printing the byte count.

Background: This is the safe, binary way to extract an attachment — the counterpart to the text-oriented GetAttachmentString. Because attachments are often binary (PDFs, images, archives), copying the raw bytes into a BinData preserves them exactly, ready to write to a file, hash, or pass to another API without any charset conversion that could corrupt the data.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Email
oleobject loo_Bd

li_Success = 0

//  Demonstrates the GetAttachmentBd method, which copies an attachment's binary data into a
//  BinData object.  The first attachment is at index 0.

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
loo_Email.Subject = "GetAttachmentBd example"

loo_Email.AddStringAttachment("notes.txt","Some notes stored in the attachment.")

//  Copy the first attachment's binary data into a BinData object.
loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Email.GetAttachmentBd(0,loo_Bd)
if li_Success = 0 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    destroy loo_Bd
    return
end if

Write-Debug "Attachment size (bytes) = " + string(loo_Bd.NumBytes)


destroy loo_Email
destroy loo_Bd