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

PowerShell
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"

$success = $false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

$mimeBytes = New-Object Chilkat.BinData
$success = $mimeBytes.LoadFile("with-attachments.eml")
if ($success -ne $true) {
    $("Failed: mimeBytes.LoadFile.")
    exit
}

$success = $mimeBytes.WriteFile("received-original.eml")
if ($success -ne $true) {
    $("Failed: mimeBytes.WriteFile.")
    exit
}

$email = New-Object Chilkat.Email
$success = $email.SetFromMimeBd($mimeBytes)
if ($success -ne $true) {
    $($email.LastErrorText)
    exit
}

$email.AddHeaderField("X-Workflow-State","reviewed")
#  GetMimeBd appends to its destination. Use a fresh buffer for this message.
$rendered = New-Object Chilkat.BinData
$success = $email.GetMimeBd($rendered)
if ($success -ne $true) {
    $($email.LastErrorText)
    exit
}

$success = $rendered.WriteFile("reviewed.eml")
if ($success -ne $true) {
    $("Failed: rendered.WriteFile.")
    exit
}

$("Archived received-original.eml and saved reviewed.eml.")