使用Powershell和EWS快速搜索今天的电子邮件

时间:2015-03-12 12:02:38

标签: email powershell exchangewebservices

我很擅长使用EWS从Exchange服务器检索电子邮件。我想以最快的方式检索今天发给我的所有电子邮件。

我对ItemView感到有点困惑。在代码1 中收集邮箱中的所有电子邮件,然后应用搜索过滤器?在代码2 中,首先订购邮件,然后只返回最新的邮件?

哪种解决方案最快才能检索到今天的电子邮件,即使邮箱可能真的满了?

代码1:

$Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($Exchservice,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)

$PropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$PropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;
$SearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsGreaterThan([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived,[system.DateTime]::Now.AddDays(-1))

$View =  New-Object Microsoft.Exchange.WebServices.Data.ItemView(100) 
$Mails = 0

do {
    $Mails = $ExchService.FindItems($Inbox.Id,$SearchFilter,$View)
    $ExchService.LoadPropertiesForItems($Mails,$PropertySet) | Out-Null

    foreach ($Mail in $Mails.Items) {
        "Received    : " + $Mail.DateTimeReceived  
        "Subject     : " + $Mail.Subject  
        "Body        : " + $Mail.Body
    }

    $View.Offset += $Mails.Items.Count
} while($Mails.MoreAvailable -eq $true)

代码2:

$Properties.add([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived)
$Properties.add([Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject)
$Properties.add([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::Body)

$View = new-object Microsoft.Exchange.WebServices.Data.ItemView(50,0,[Microsoft.Exchange.WebServices.Data.OffsetBasePoint]::Beginning)
$View.Traversal = [Microsoft.Exchange.WebServices.Data.ItemTraversal]::Shallow
$View.PropertySet=$Properties
$View.OrderBy.add([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived,[Microsoft.Exchange.WebServices.Data.SortDirection]::Ascending)

0 个答案:

没有答案