none
[EWS] [E2010] Search All Users for specific Subject/Body combination. RRS feed

  • Question

  • The goal is that when/if something comes into the company that should be deleted (spam that wasn't caught that has a high chance of someone clicking on it), that I can do a quick search and delete all copies of the message.

     

    Currently, the way that I do this is to loop through all known e-mail addresses (looked up from a database, unsure how to get this directly from EWS) and create a search folder for the user with the proper values.  Once done with that mailbox, I delete the search folder.

    I went the search folder route because it seemed to be much faster than looping through e-mail e-mail; however, I have no clue if it is the optimal way of doing it.  It seems to take quite some time to complete, (less time when I only used Subject as a criteria).

     

    Here is some snippits of my current code in case that helps.

    Dim mySearch As SearchFilter
            mySearch = New SearchFilter.SearchFilterCollection(
              LogicalOperator.And,
              New SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, Now.AddDays(-7)),
              New SearchFilter.ContainsSubstring(ItemSchema.Subject, txt_Subject.Text),
              New SearchFilter.ContainsSubstring(ItemSchema.Body, txt_Body.Text)
              )
    
            Dim mySearchFolder As SearchFolder
            mySearchFolder = New SearchFolder(Service)
            mySearchFolder.SearchParameters.RootFolderIds.Add(WellKnownFolderName.Root)
            mySearchFolder.SearchParameters.Traversal = SearchFolderTraversal.Deep
            mySearchFolder.SearchParameters.SearchFilter = mySearch
            mySearchFolder.DisplayName = "Temp"
    
            Try
              mySearchFolder.Save(WellKnownFolderName.SearchFolders)
            Catch ex As Exception
              Dim rootFolderID As New FolderId(WellKnownFolderName.SearchFolders)
              Dim folderView As New FolderView(10000)
              folderView.Traversal = FolderTraversal.Deep
    
              Dim ffResponse As FindFoldersResults
              ffResponse = Service.FindFolders(rootFolderID, folderView)
    
              For Each Folder In ffResponse.Folders
                If Folder.DisplayName = "Temp" Then
                  Folder.Delete(DeleteMode.HardDelete)
                  Exit For
                End If
              Next
              mySearchFolder.Save(WellKnownFolderName.SearchFolders)
            End Try
    
    
            Dim findResults2 As FindItemsResults(Of Item) = mySearchFolder.FindItems(New ItemView(200))
            Try
              Service.LoadPropertiesForItems(findResults2, New PropertySet(ItemSchema.ParentFolderId, ItemSchema.Body, ItemSchema.Subject, ItemSchema.DisplayTo, ItemSchema.DisplayCc, ItemSchema.DateTimeReceived, ItemSchema.DateTimeSent))
              For Each Item In findResults2.Items
                Dim mailBody As String = Item.Body.Text
                Dim myEmail As EmailMessage = EmailMessage.Bind(Service, Item.Id, New PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.ParentFolderId))
                lbl_Email_Output.Text += "<br><b>Mailbox: </b>" & datareader("Email_Address")
    
                Dim rootFolderID As New FolderId(WellKnownFolderName.Root)
                Dim folderView As New FolderView(10000)
                folderView.Traversal = FolderTraversal.Deep
                Dim ffResponse As FindFoldersResults
                ffResponse = Service.FindFolders(rootFolderID, folderView)
                For Each Folder In ffResponse.Folders
                  If Folder.Id.ToString = Item.ParentFolderId.ToString Then
                    lbl_Email_Output.Text += "<br><b>Folder: </b>" & Folder.DisplayName
                  End If
                Next
    
                lbl_Email_Output.Text += "<br><b>From: </b>" & myEmail.From.Name & " - " & myEmail.From.Address
                lbl_Email_Output.Text += "<br><b>To: </b>" & Item.DisplayTo
                lbl_Email_Output.Text += "<br><b>CC: </b>" & Item.DisplayCc
                lbl_Email_Output.Text += "<br><b>Date Sent: </b>" & Item.DateTimeSent
                lbl_Email_Output.Text += "<br><b>Date Received: </b>" & Item.DateTimeReceived
                lbl_Email_Output.Text += "<br><b>" & Item.Subject & "<br></b>" & mailBody & "<br><hr noshade="""" size=""1"" color=""#CCCCCC"">"
              Next
            Catch ex As Exception
            End Try
    
            Try
              mySearchFolder.Delete(DeleteMode.HardDelete)
            Catch ex As Exception
              Threading.Thread.Sleep(200)
              Try
                mySearchFolder.Delete(DeleteMode.HardDelete)
              Catch ex2 As Exception
                Threading.Thread.Sleep(1000)
                Try
                  mySearchFolder.Delete(DeleteMode.HardDelete)
                Catch ex3 As Exception
                End Try
              End Try
            End Try
    

    Monday, May 2, 2011 3:48 PM

All replies