I am able to search and find an email if it exists in the INBOX.
But how do I search if the Same email is in a different folder.
I am traversing to the root and going down the list, but when I do the search it does not return anything.
I received a few tips from https://docs.microsoft.com/en-us/archive/blogs/santhse/get-readstatus. It works for the most part, although there are creating a folder "TEMP-MSG-ID" in the code, and when running this code over 50,000+ users the folder creation breaks infrequently. So I am trying to find an approach that works correctly for all mailboxes.
Here is my code:
$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 FilesMicrosoftExchangeWeb Services2.2Microsoft.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)
question from:
https://stackoverflow.com/questions/65848154/powershell-using-ews-to-search-for-email-which-is-not-in-the-inbox 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…