Sample code for 30+ languages & platforms
Xbase++ 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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oMimeBytes
LOCAL oEmail
LOCAL oRendered

nSuccess := 0

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

oMimeBytes := CreateObject("Chilkat.BinData")
nSuccess := oMimeBytes:LoadFile("with-attachments.eml")
IF (nSuccess != 1)
    ? "Failed: mimeBytes.LoadFile."
    oMimeBytes:destroy()
    RETURN
ENDIF

nSuccess := oMimeBytes:WriteFile("received-original.eml")
IF (nSuccess != 1)
    ? "Failed: mimeBytes.WriteFile."
    oMimeBytes:destroy()
    RETURN
ENDIF

oEmail := CreateObject("Chilkat.Email")
nSuccess := oEmail:SetFromMimeBd(oMimeBytes)
IF (nSuccess != 1)
    ? oEmail:LastErrorText
    oMimeBytes:destroy()
    oEmail:destroy()
    RETURN
ENDIF

oEmail:AddHeaderField("X-Workflow-State", "reviewed")
//  GetMimeBd appends to its destination. Use a fresh buffer for this message.
oRendered := CreateObject("Chilkat.BinData")
nSuccess := oEmail:GetMimeBd(oRendered)
IF (nSuccess != 1)
    ? oEmail:LastErrorText
    oMimeBytes:destroy()
    oEmail:destroy()
    oRendered:destroy()
    RETURN
ENDIF

nSuccess := oRendered:WriteFile("reviewed.eml")
IF (nSuccess != 1)
    ? "Failed: rendered.WriteFile."
    oMimeBytes:destroy()
    oEmail:destroy()
    oRendered:destroy()
    RETURN
ENDIF

? "Archived received-original.eml and saved reviewed.eml."

oMimeBytes:destroy()
oEmail:destroy()
oRendered:destroy()