Sample code for 30+ languages & platforms
PowerBuilder

Attach a File to a Multipart MIME Entity

See more MIME Examples

Demonstrates the Chilkat Mime.AppendPartFromFile method, which reads a local file, creates an attachment part, and appends it. The only argument is the file path; the content headers are chosen from the filename extension.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: This is the one-call way to add an attachment: Chilkat reads the file, infers its Content-Type from the extension, base64-encodes binary content, and appends it as a child part — typically inside a multipart/mixed. It is the building block behind adding attachments to an email, saving you from constructing the part and setting its headers by hand.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Mime
oleobject loo_BodyPart

li_Success = 0

//  Demonstrates the Mime.AppendPartFromFile method, which reads a local file, creates an attachment
//  part, and appends it.  The only argument is the file path; the content headers are chosen from
//  the filename extension.

loo_Mime = create oleobject
li_rc = loo_Mime.ConnectToNewObject("Chilkat.Mime")
if li_rc < 0 then
    destroy loo_Mime
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Mime.NewMultipartMixed()
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    return
end if

//  A message body part.
loo_BodyPart = create oleobject
li_rc = loo_BodyPart.ConnectToNewObject("Chilkat.Mime")

li_Success = loo_BodyPart.SetBodyFromPlainText("Please see the attached files.")
if li_Success = 0 then
    Write-Debug loo_BodyPart.LastErrorText
    destroy loo_Mime
    destroy loo_BodyPart
    return
end if

li_Success = loo_Mime.AppendPart(loo_BodyPart)
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    destroy loo_BodyPart
    return
end if

//  Attach files directly.
li_Success = loo_Mime.AppendPartFromFile("qa_data/report.pdf")
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    destroy loo_BodyPart
    return
end if

li_Success = loo_Mime.AppendPartFromFile("qa_data/photo.jpg")
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    destroy loo_BodyPart
    return
end if

Write-Debug "Number of parts: " + string(loo_Mime.NumParts)


destroy loo_Mime
destroy loo_BodyPart