Sample code for 30+ languages & platforms
PowerShell

Download MIME Source of Emails in IMAP Mailbox

Demonstrates how to download the MIME source for emails on an IMAP server.

Chilkat PowerShell Downloads

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

$success = $false

# This example assumes 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("myLogin","myPassword")
if ($success -eq $false) {
    $($imap.LastErrorText)
    exit
}

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

# The NumMessages property contains the number of messages in the selected mailbox.
$numMessages = $imap.NumMessages
if ($numMessages -eq 0) {
    $("No messages exist in the Inbox.")
    exit
}

$sbMime = New-Object Chilkat.StringBuilder

for ($seqNum = 1; $seqNum -le $numMessages; $seqNum++) {
    $sbMime.Clear()
    $success = $imap.FetchSingleAsMimeSb($seqNum,$false,$sbMime)
    if ($success -eq $false) {
        $($imap.LastErrorText)
        exit
    }

    # The MIME source of the downloaded email is contained in sbMime.
}

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