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

Find and save an attachment and list inline images

See more Email Object Examples

Find an attachment by filename and media type, then save its decoded bytes to a path you choose. Enumerate inline resources separately to read their Content-IDs.

Chilkat PowerShell Downloads

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

$success = $false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

$email = New-Object Chilkat.Email
$success = $email.LoadEml("with-attachments.eml")
if ($success -ne $true) {
    $($email.LastErrorText)
    exit
}

$filename = New-Object Chilkat.StringBuilder
$mediaType = New-Object Chilkat.StringBuilder
$found = $false
$i = 0
while (($i -lt $email.NumAttachments)) {
    $filename.SetString($email.GetAttachmentFilename($i))
    $mediaType.SetString($email.GetAttachmentContentType($i))
    if (($filename.ContentsEqual("agenda.csv",$false) -eq $true) -and ($mediaType.ContentsEqual("text/csv",$false) -eq $true) -and ($found -eq $false)) {
        $data = New-Object Chilkat.BinData
        $success = $email.GetAttachmentBd($i,$data)
        if ($success -ne $true) {
            $($email.LastErrorText)
            exit
        }

        $success = $data.WriteFile("received-agenda.csv")
        if ($success -ne $true) {
            $("Failed: data.WriteFile.")
            exit
        }

        $("Saved attachment: " + $data.NumBytes + " bytes.")
        $found = $true
    }

    $i = $i + 1
}

if ($found -ne $true) {
    $("No matching attachment.")
}

$i = 0
while (($i -lt $email.NumRelatedItems)) {
    $("Related: " + $email.GetRelatedContentType($i) + "  Content-ID: " + $email.GetRelatedContentID($i))
    $i = $i + 1
}