Sample code for 30+ languages & platforms
PowerShell

Fetch Single Email by UID or Sequence Number

Assuming the UID is known, download a single email by UID from an IMAP mail server.

Chilkat PowerShell Downloads

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

$success = $false

# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

$imap = New-Object Chilkat.Imap

# Connect to an IMAP server.
# Use TLS
$imap.Ssl = $true
$imap.Port = 993
$success = $imap.Connect("imap.example.com")
if ($success -eq $false) {
    $($imap.LastErrorText)
    exit
}

# Login
$success = $imap.Login("***","***")
if ($success -eq $false) {
    $($imap.LastErrorText)
    exit
}

# Select an IMAP mailbox
$success = $imap.SelectMailbox("Inbox")
if ($success -eq $false) {
    $($imap.LastErrorText)
    exit
}

$email = New-Object Chilkat.Email

$uid = 2014
$isUid = $true

$success = $imap.FetchEmail($false,$uid,$isUid,$email)
if ($success -eq $false) {
    $($imap.LastErrorText)
    exit
}

# Display the From and Subject
$($email.FromAddress)
$($email.Subject)

# Display the Body property, which is the default body.
# If an email has an HTML body, the Body property contains
# the HTML source.  Otherwise it contains the plain-text
# body.
$("---- EMAIL BODY ----")
$($email.Body)
$("--------------------")

# Display the recipients:

for ($j = 0; $j -le $email.NumTo - 1; $j++) {
    $($email.GetToName($j) + ", " + $email.GetToAddr($j))
}

for ($j = 0; $j -le $email.NumCC - 1; $j++) {
    $($email.GetCcName($j) + ", " + $email.GetCcAddr($j))
}

# Show the total size of the email, including body and attachments:
$($email.Size)

# When a full email is downloaded, we would use the
# email.NumAttachments property in conjunction with the
# email.GetMailAttach* methods.
# However, when an email object contains only the header,
# we need to access the attachment info differently:
$numAttach = $imap.GetMailNumAttach($email)
$($numAttach)

for ($j = 0; $j -le $numAttach - 1; $j++) {
    $($imap.GetMailAttachFilename($email,$j))
    $attachSize = $imap.GetMailAttachSize($email,$j)
    $("    size = " + $attachSize + " bytes")
}

$("--")

# Disconnect from the IMAP server.
$success = $imap.Disconnect()