MailKit mit Powershell nutzen um Emails abzufragen via IMAP

Ich habe ja bereits in der Vergangenheit über IMAP mit IMAPX geschrieben. Leider wird IMAPX nicht mehr weiterentickelt, aber es gibt MailKit als alternative.

Daher noch ein kleines Skript zum Abfragen vom Posteingang:

#add libs
Add-Type -Path "c:\temp\imap\BouncyCastle.Crypto.dll"
Add-Type -Path "c:\temp\imap\MimeKit.dll"
Add-Type -Path "c:\temp\imap\MailKit.dll"

#init
$cnn = New-Object MailKit.Net.Imap.ImapClient

#connect
$cnn.Connect($mailserver, $port)
$cnn.Authenticate($username,$password)
$cnn.Inbox.Open([MailKit.FolderAccess]::ReadWrite)

#set up and query for all emails
$query = [MailKit.Search.SearchQuery]::NotDeleted        
$uids = $cnn.Inbox.Search($query) 

#each mail at the inbox
foreach($uid in $uids.GetEnumerator()){

	#pull email from server
	$m = $cnn.Inbox.GetMessage($uid)

	#each attachment of the current mail
	foreach($a in $m.Attachments){
		$a.fileName
	}

	$m.Subject
	$m.from.address
	$m.from.name
	$m.TextBody

}

$cnn.Disconnect($true)

http://www.agile-coding.net/mailkit-mit-powershell-nutzen-um-emails-abzufragen-via-imap/