PowerShell
PowerShell
Fetch a MessageSet into an EmailBundle
See more IMAP Examples
Demonstrates the Chilkat Imap.FetchMsgSet method, which downloads the messages identified by a MessageSet and appends them to an EmailBundle. The arguments are headersOnly, the MessageSet, and the EmailBundle (which is not cleared). This example searches for unseen messages and downloads the whole set.
Background: This is the natural pairing with
QueryMbx: search to get a MessageSet of matching identifiers, then download exactly those messages in one call. Fetching a set is far more efficient than looping one message at a time, and it returns an EmailBundle you iterate with MessageCount and EmailAt. Pass headersOnly = true to pull just headers for a fast preview list.Chilkat PowerShell Downloads
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"
$success = $false
# Demonstrates the Imap.FetchMsgSet method, which downloads the messages identified by a
# MessageSet and appends them to an EmailBundle. The 1st argument is headersOnly, the 2nd
# is the MessageSet, and the 3rd is the EmailBundle (which is not cleared).
$imap = New-Object Chilkat.Imap
$imap.Ssl = $true
$imap.Port = 993
$success = $imap.Connect("imap.example.com")
if ($success -eq $false) {
$($imap.LastErrorText)
exit
}
$success = $imap.Login("user@example.com","myPassword")
if ($success -eq $false) {
$($imap.LastErrorText)
exit
}
$success = $imap.SelectMailbox("Inbox")
if ($success -eq $false) {
$($imap.LastErrorText)
exit
}
# Find the messages to download (here, the unseen messages by UID).
$msgSet = New-Object Chilkat.MessageSet
$success = $imap.QueryMbx("UNSEEN",$true,$msgSet)
if ($success -eq $false) {
$($imap.LastErrorText)
exit
}
# Download the messages in the set (full bodies) into a bundle.
$bundle = New-Object Chilkat.EmailBundle
$success = $imap.FetchMsgSet($false,$msgSet,$bundle)
if ($success -eq $false) {
$($imap.LastErrorText)
exit
}
$n = $bundle.MessageCount
$("Fetched " + $n + " messages.")
$email = New-Object Chilkat.Email
for ($i = 0; $i -le $n - 1; $i++) {
$success = $bundle.EmailAt($i,$email)
$($email.Subject)
}
$success = $imap.Disconnect()
if ($success -eq $false) {
$($imap.LastErrorText)
exit
}