views:

266

answers:

2

Trying to Read a string in Powershell from an email with IMAP connect:

I use the Mail.dll from http://www.lesnikowski.com/mail/ Docu: http://www.lesnikowski.com/mail/documentation/

I want to search for a specific Subject.

What i have so far:

[Reflection.Assembly]::LoadFile("c:\mail.dll")
$imap = new-object Lesnikowski.Client.IMAP.Imap

$imap.Connect("192.168.0.1")
$imap.User = "user"
$imap.Password = "xxxx"
$imap.login()
  $imap.Select("Inbox.folder.subfolder")    #instead of $imap.selectinbox() i select a subfolder

  $imap.GetMessage() 

$imap.GetMessage() now lists one email, i think the last one.. but i want one with a specific subject.

The Doku gives following example but i am not able to handle this im Powershell:

List<long> uids = imap.Search().Where(Expression.Subject("report")).Sort(SortBy.Date()).GetUIDList();

I think i probably went in trouble with my tests because the "Where" is also a native Posh-Command... It always ended in a missing ) error ...

A: 

It looks like these methods are neither static extension methods nor generic methods. I'd say you just need help converting this C# syntax to PowerShell:

List<long> uids = imap.Search().Where(Expression.Subject("report")).Sort(SortBy.Date()).GetUIDList();

First up, the C# generic syntax doesn't work in PowerShell and the syntax for invoking static methods is different. Try something like this:

$report = [Lesnikowski.Client.IMAP.Expression]::Subject("report")
$sorter = [Lesnikowski.Client.IMAP.SortBy]::Date()
$uids   = $imap.Search().Where($report).Sort($sorter).GetUIDList()
Keith Hill
+1  A: 

I'm not a PowerShell expert, but this works:

$Expression = [Lesnikowski.Client.IMAP.Expression]
$uids = $imap.Search().Where($Expression::Subject("report")).GetUIDList()
foreach ($uid in $uids ) { $imap.GetMessageByUID($uid) }

Please also note that Sort extension is rarely supported by IMAP servers.

Pawel Lesnikowski
This worked! Thank you
icnivad