我可以搜索并找到收件箱中存在的电子邮件。 但是如何搜索相同的电子邮件是否位于不同的文件夹中。 我正在遍历根目录并沿着列表向下移动,但是当我进行搜索时,它没有返回任何内容。 我从 https://docs.microsoft.com/en-us/archive/blogs/santhse/get-readstatus 那里收到了一些提示。尽管在代码中创建了一个文件夹“TEMP-MSG-ID”,但它在大多数情况下都有效,并且当超过 50,000 多个用户运行此代码时,文件夹创建很少中断。所以我试图找到一种适用于所有邮箱的方法。 这是我的代码:
$MsgSubject = "Complete Language"
$AdminName = "a34d4557@wyx.com"
$Pass = "password"
$User_Email = "hil@wyx.com"
$credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Pass
$AdminPass = $credential.GetNetworkCredential().password
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
[void][Reflection.Assembly]::LoadFile($dllpath)
$service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService
$Service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials($AdminName,$AdminPass)
$TestUrlCallback = {
param ([string] $url)
if ($url -eq "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml") {$true} else {$false}
}
$service.AutodiscoverUrl($User_Email,$TestUrlCallback)
$PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$PropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text
$mb = New-Object Microsoft.Exchange.WebServices.Data.Mailbox($User_Email)
$InboxFolder = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox, $mb)
#---------------------------------------------------
# ----- If email is in Inbox -- Works correctly ----
#---------------------------------------------------
$numOfEmailsToRead = 100
$index = 0
$view = New-Object Microsoft.Exchange.WebServices.Data.ItemView($numOfEmailsToRead,$index)
$search1 = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+ContainsSubstring([Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject, $MsgSubject, [Microsoft.Exchange.WebServices.Data.ContainmentMode]::Substring, [Microsoft.Exchange.WebServices.Data.ComparisonMode]::IgnoreCase)
$UserMsgRecd = $service.FindItems($InboxFolder,$search1,$view)
#----------------------------------------------------------------------------------------------
# ----- If email is not in the Inbox but different folder -- Need to get this part to work ----
#----------------------------------------------------------------------------------------------
$RootFolder = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root, $mb)
$EWSParentFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$RootFolder)
$FolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(100)
$FolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
$MailboxFolderList = $EWSParentFolder.FindFolders($FolderView)
$UserMsgRecd = $service.FindItems($EWSParentFolder,$search1,$FolderView)
答案 0 :(得分:0)
我建议您使用 AllItems Search 文件夹,这是图表中的 /messages 端点使用的。要在 EWS 中使用它,您需要先搜索它以找到 FolderId,然后您可以在 FindItems 方法中使用它,例如
$MsgSubject = "Complete Language"
$AdminName = "a34d4557@wyx.com"
$Pass = "password"
$User_Email = "hil@wyx.com"
$credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Pass
$AdminPass = $credential.GetNetworkCredential().password
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
[void][Reflection.Assembly]::LoadFile($dllpath)
$service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService
$Service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials($AdminName,$AdminPass)
$TestUrlCallback = {
param ([string] $url)
if ($url -eq "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml") {$true} else {$false}
}
$service.AutodiscoverUrl($User_Email,$TestUrlCallback)
$PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$PropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text
$mb = New-Object Microsoft.Exchange.WebServices.Data.Mailbox($User_Email)
$folderidcnt = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root, $mb)
$fvFolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1)
$fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Shallow;
$SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName, "allitems")
$findFolderResults = $Service.FindFolders($folderidcnt, $SfSearchFilter, $fvFolderView)
$numOfEmailsToRead = 100
$index = 0
$view = New-Object Microsoft.Exchange.WebServices.Data.ItemView($numOfEmailsToRead,$index)
$search1 = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+ContainsSubstring([Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject, $MsgSubject, [Microsoft.Exchange.WebServices.Data.ContainmentMode]::Substring, [Microsoft.Exchange.WebServices.Data.ComparisonMode]::IgnoreCase)
$UserMsgRecd = $service.FindItems($findFolderResults.Folders[0] ,$search1,$view)