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

Preserve the original email and save a modified copy

See more Email Object Examples

Use with-attachments.eml as a stand-in for bytes from an IMAP fetch or a message queue. Preserve those bytes first. Parse them into an Email object, add a local processing header, and serialize the result into a separate buffer. The archived input remains suitable for later byte-level inspection.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_MimeBytes
oleobject loo_Email
oleobject loo_Rendered

li_Success = 0

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

loo_MimeBytes = create oleobject
li_rc = loo_MimeBytes.ConnectToNewObject("Chilkat.BinData")
if li_rc < 0 then
    destroy loo_MimeBytes
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_MimeBytes.LoadFile("with-attachments.eml")
if li_Success <> 1 then
    Write-Debug "Failed: mimeBytes.LoadFile."
    destroy loo_MimeBytes
    return
end if

li_Success = loo_MimeBytes.WriteFile("received-original.eml")
if li_Success <> 1 then
    Write-Debug "Failed: mimeBytes.WriteFile."
    destroy loo_MimeBytes
    return
end if

loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

li_Success = loo_Email.SetFromMimeBd(loo_MimeBytes)
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_MimeBytes
    destroy loo_Email
    return
end if

loo_Email.AddHeaderField("X-Workflow-State","reviewed")
//  GetMimeBd appends to its destination. Use a fresh buffer for this message.
loo_Rendered = create oleobject
li_rc = loo_Rendered.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Email.GetMimeBd(loo_Rendered)
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_MimeBytes
    destroy loo_Email
    destroy loo_Rendered
    return
end if

li_Success = loo_Rendered.WriteFile("reviewed.eml")
if li_Success <> 1 then
    Write-Debug "Failed: rendered.WriteFile."
    destroy loo_MimeBytes
    destroy loo_Email
    destroy loo_Rendered
    return
end if

Write-Debug "Archived received-original.eml and saved reviewed.eml."


destroy loo_MimeBytes
destroy loo_Email
destroy loo_Rendered