Sample code for 30+ languages & platforms
PowerBuilder

Get the Nth Binary Part of a Content-Type into BinData

See more Email Object Examples

Demonstrates the Chilkat Email.GetNthBinaryPartOfTypeBd method, which loads the binary bytes of the Nth MIME sub-part matching a content-type pattern into a BinData object. The arguments are the zero-based index among the matching parts, the content-type pattern, an inlineOnly flag, an excludeAttachments flag, and the BinData that receives the bytes. This example extracts the first image/png part.

Background: This is the binary, type-targeted way to pull a specific part out of a message — ideal for extracting, say, every image/png or the one application/pdf from a complex MIME tree without caring whether it is an attachment, an inline image, or a body part. Reading into a BinData keeps the raw bytes exact, ready to save, hash, or re-transmit.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Email
oleobject loo_BdImage
oleobject loo_Bd

li_Success = 0

//  Demonstrates the GetNthBinaryPartOfTypeBd method, which loads the binary bytes of the Nth
//  MIME sub-part matching a Content-Type pattern into a BinData object.  The arguments are
//  the zero-based index among matching parts, the Content-Type pattern, inlineOnly,
//  excludeAttachments, and the BinData that receives the bytes.

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 = "GetNthBinaryPartOfTypeBd example"
loo_Email.Body = "See the attached image."

//  Load the image from a file into a BinData object and attach it (binary data belongs
//  in a BinData, never in a string).
loo_BdImage = create oleobject
li_rc = loo_BdImage.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_BdImage.LoadFile("qa_data/images/photo.png")
if li_Success = 0 then
    Write-Debug loo_BdImage.LastErrorText
    destroy loo_Email
    destroy loo_BdImage
    return
end if

li_Success = loo_Email.AddAttachmentBd("photo.png",loo_BdImage,"image/png")
if li_Success = 0 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    destroy loo_BdImage
    return
end if

//  Load the bytes of the first (index 0) image/png part into a BinData object.
loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Email.GetNthBinaryPartOfTypeBd(0,"image/png",0,0,loo_Bd)
if li_Success = 0 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    destroy loo_BdImage
    destroy loo_Bd
    return
end if

Write-Debug "image/png part size (bytes) = " + string(loo_Bd.NumBytes)

//  Note: The path "qa_data/images/photo.png" is a relative local filesystem path,
//  relative to the current working directory of the running application.


destroy loo_Email
destroy loo_BdImage
destroy loo_Bd