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

Preview POP3 messages and download a complete email

See more Email Object Examples

Read a short preview for each of up to five messages into an EmailBundle, print their subjects, and download the complete first entry into a separate Email. Configure a POP3 account that supports UIDL and TOP. All messages stay on the server.

Chilkat PowerShell Downloads

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

$success = $false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

$mail = New-Object Chilkat.MailMan
$mail.MailHost = "pop.example.com"
$mail.MailPort = 995
$mail.PopSsl = $true
$mail.RequireSslCertVerify = $true
$mail.PopUsername = "MAIL_USER"
$mail.PopPassword = "MAIL_PASSWORD"
$previews = New-Object Chilkat.EmailBundle
#  Keep the messages and request minimal previews. Range indexes are zero-based and inclusive.
$success = $mail.FetchRange($true,$true,0,0,4,$previews)
if ($success -ne $true) {
    $($mail.LastErrorText)
    exit
}

if ($previews.MessageCount -eq 0) {
    $("There are no messages to preview.")
    $success = $mail.Pop3EndSession()
    if ($success -ne $true) {
        $($mail.LastErrorText)
        exit
    }

    exit
}

$header = New-Object Chilkat.Email
$i = 0
while (($i -lt $previews.MessageCount)) {
    $success = $previews.EmailAt($i,$header)
    if ($success -ne $true) {
        $($previews.LastErrorText)
        exit
    }

    $($header.Uidl + "  " + $header.Subject)
    $i = $i + 1
}

#  A UI could use its selected row here. FetchFull uses the preview's POP3 identity.
$success = $previews.EmailAt(0,$header)
if ($success -ne $true) {
    $($previews.LastErrorText)
    exit
}

$email = New-Object Chilkat.Email
$success = $mail.FetchFull($header,$email)
if ($success -ne $true) {
    $($mail.LastErrorText)
    exit
}

$success = $mail.Pop3EndSession()
if ($success -ne $true) {
    $($mail.LastErrorText)
    exit
}

$("Full message: " + $email.Subject + "  Files: " + $email.NumAttachments)

$success = $email.SaveEml("pop-received.eml")
if ($success -ne $true) {
    $($email.LastErrorText)
    exit
}

$("Saved pop-received.eml.")