Sample code for 30+ languages & platforms
PowerBuilder

Add an Attachment from a BinData Object

See more Email Object Examples

Demonstrates the Chilkat Email.AddAttachmentBd method, which adds an attachment using the contents of a BinData object. The first argument is the attachment filename, the second is the BinData, and the third is the content type — if empty, it is inferred from the filename extension. This example loads a PDF into a BinData and attaches it.

Background: BinData is Chilkat's container for raw binary data. Attaching from a BinData is the right approach when the file's bytes are already in memory — generated on the fly, downloaded, or read from a database — rather than sitting on disk (which would use AddFileAttachment). Chilkat Base64-encodes the bytes into the message automatically.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Email
oleobject loo_Bd

li_Success = 0

//  Demonstrates the AddAttachmentBd method, which adds an attachment using the contents of a
//  BinData object.  The first argument is the attachment filename, the second is the BinData
//  object, and the third is the content type (inferred from the filename extension if empty).

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 = "Attach from BinData"
loo_Email.Body = "Please see the attached file."

//  Load a file into a BinData object, then attach it.
loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Bd.LoadFile("qa_data/attachments/report.pdf")
if li_Success = 0 then
    Write-Debug loo_Bd.LastErrorText
    destroy loo_Email
    destroy loo_Bd
    return
end if

li_Success = loo_Email.AddAttachmentBd("report.pdf",loo_Bd,"application/pdf")
if li_Success = 0 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    destroy loo_Bd
    return
end if

Write-Debug "NumAttachments = " + string(loo_Email.NumAttachments)

//  Note: The path "qa_data/attachments/report.pdf" is a relative local filesystem path,
//  relative to the current working directory of the running application.


destroy loo_Email
destroy loo_Bd