VB.Net EWS如何开始搜索指定子文件夹中的邮件项目并查找所有子文件夹

VB.Net EWS如何开始搜索指定子文件夹中的邮件项目并查找所有子文件夹,vb.net,exchangewebservices,Vb.net,Exchangewebservices,使用VB,我希望能够开始从特定的邮件子文件夹中搜索电子邮件的日期和主题,并返回所有邮件项目,这是我到目前为止所做的 由于某些项目落在outlook365的“其他”中,您还需要能够搜索它们 Dim searchFilterCollection As List(Of SearchFilter) = New List(Of SearchFilter)() Dim searchdate As DateTime = New DateTime(2019, 11, 19) 'Year, month, day

使用VB,我希望能够开始从特定的邮件子文件夹中搜索电子邮件的日期和主题,并返回所有邮件项目,这是我到目前为止所做的

由于某些项目落在outlook365的“其他”中,您还需要能够搜索它们

Dim searchFilterCollection As List(Of SearchFilter) = New List(Of SearchFilter)()

Dim searchdate As DateTime = New DateTime(2019, 11, 19) 'Year, month, day
Dim greaterthanfilter As SearchFilter = New SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, searchdate)
Dim lessthanfilter As SearchFilter = New SearchFilter.IsLessThan(ItemSchema.DateTimeReceived, searchdate.AddDays(1))

searchFilterCollection.Add(New SearchFilter.SearchFilterCollection(LogicalOperator.And, greaterthanfilter, lessthanfilter))
searchFilterCollection.Add(New SearchFilter.IsEqualTo(FolderSchema.DisplayName, "My Folder"))
  searchFilterCollection.Add(New SearchFilter.ContainsSubstring(ItemSchema.Subject, "Test Subject"))


    ' Create the search filter.
    Dim searchFilter As SearchFilter = New SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection.ToArray())
    ' Create a view with a page size of 50.
    Dim view As New ItemView(50)

    'Identify the Subject and DateTimeReceived properties to return.
    'Indicate that the base property will be the item identifier
    view.PropertySet = (New PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, ItemSchema.DateTimeReceived))

    ' Order the search results by the DateTimeReceived in descending order.
    view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending)

    ' Set the traversal to shallow. (Shallow is the default option; other options are Associated and SoftDeleted.)
    view.Traversal = ItemTraversal.Shallow

    ' Send the request to search the Inbox and get the results.
    Dim findResults As FindItemsResults(Of Item) = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view)

    For Each item As Item In findResults
        Dim message As EmailMessage = EmailMessage.Bind(exchange, item.Id)
        Dim listitem As ListViewItem = New ListViewItem({message.DateTimeReceived.ToString(), message.From.Name.ToString() & "(" + message.From.Address.ToString() & ")", message.Subject, (If((message.HasAttachments), "Yes", "No")), message.Id.ToString()})
        lstMsg.Items.Add(listitem)
    Next

    If findResults.Items.Count <= 0 Then
        lstMsg.Items.Add("No Messages found!!")
    End If
Dim searchFilterCollection As List(属于SearchFilter)=新列表(属于SearchFilter)()
Dim searchdate As DateTime=新日期时间(2019、11、19)“年、月、日
Dim greaterthanfilter As SearchFilter=New SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived,searchdate)
Dim lessthanfilter As SearchFilter=New SearchFilter.IsLessThan(ItemSchema.DateTimeReceived,searchdate.AddDays(1))
添加(新的SearchFilter.searchFilterCollection(LogicalOperator.And,greaterthanfilter,lessthanfilter))
添加(新的SearchFilter.IsEqualTo(FolderSchema.DisplayName,“我的文件夹”))
添加(新的SearchFilter.ContainsSubstring(ItemSchema.Subject,“测试主题”))
'创建搜索筛选器。
Dim searchFilter As searchFilter=新建searchFilter.SearchFilterCollection(LogicalOperator.And,SearchFilterCollection.ToArray())
'创建页面大小为50的视图。
将视图变暗为新项目视图(50)
'标识要返回的主题和DateTimeReceived属性。
'指示基本属性将是项标识符
view.PropertySet=(新的PropertySet(BasePropertySet.IdOnly,ItemSchema.Subject,ItemSchema.DateTimeReceived))
'按DateTimeReceived降序排列搜索结果。
view.OrderBy.Add(ItemSchema.DateTimeReceived,SortDirection.Descending)
'将遍历设置为“浅”。(浅层是默认选项;其他选项关联并删除。)
view.Traversal=ItemTraversal.Shallow
'发送请求以搜索收件箱并获取结果。
将findResults设置为FindItemsResults(项目的)=service.FindItems(WellKnownFolderName.Inbox、searchFilter、view)
作为findResults中的项目的每个项目
Dim message As EmailMessage=EmailMessage.Bind(exchange,item.Id)
Dim listitem As ListViewItem=新ListViewItem({message.DateTimeReceived.ToString(),message.From.Name.ToString()&“(“+message.From.Address.ToString()&”)”,message.Subject,(如果((message.HasAttachments),“Yes”,“No”)),message.Id.ToString())
lstMsg.Items.Add(listitem)
下一个

如果findResults.Items.Count下面的代码允许我搜索包含“其他”文件夹的“所有项目”文件夹,并按日期仅检索特定的电子邮件

注意:在此处找到代码:


好的-这就是你到目前为止所做的。有问题吗?它有用吗?如果不起作用,错误或意外输出在哪里?不,如果我删除了2个筛选器searchFilterCollection.Add(New SearchFilter.IsEqualTo(FolderSchema.DisplayName,“我的文件夹”))searchFilterCollection.Add(New SearchFilter.ContainsSubstring(ItemSchema.Subject,“测试主题”)),则它不起作用然后它从主收件箱带回当天的电子邮件,我想以某种方式获取它,以便我可以在收件箱及其子文件夹中搜索给定主题的文件夹,但我无法将其从指定的子文件夹开始。我发现一个解决方案不理想,但确实有效,在此行之前,将findResults作为FindItemsResults(项目的)进行Dim findResults=service.FindItems(WellKnownFolderName.Inbox、searchFilter、view),我有一个函数调用,该函数调用获取文件夹名称的文件夹详细信息,该名称在此行中用作myfolder.id而不是wellknown文件夹,但您必须获取每个文件夹的id,我想要的是将staruing文件夹设置为从搜索到某个内容,然后仅搜索该文件夹和所有子文件夹。我遇到了一个问题,即某些电子邮件登录到“其他”邮箱而未聚焦,我如何在该邮箱中搜索,我尝试过搜索其他文件夹,也尝试过我找到的其他一些代码,这些代码可以定位“Allitems”文件夹,但它仍然不显示“other”中的任何电子邮件。您可以通过此链接将这些信息添加到问题中:-这将改进您的问题并提高您获得答案的机会。
Private Sub FindAllEmailByDate()

    ' ============== Setup Exchange connection =================================================================================
    'Add a valid EWS service end point here or user Autodiscover
    service.Url = New Uri("https://outlook.office365.com/EWS/Exchange.asmx")
    ' Connect by using the default credentials of the authenticated user.
    service.UseDefaultCredentials = False
    'Add a valid user credentials
    service.Credentials = New WebCredentials("EMAIL", "PASSWORD")

    'To address the SSL challenge
    ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateCertificate)


    ' ============== Gets ALLITEMS folder details =================================================================================
    Dim allFoldersType As ExtendedPropertyDefinition = New ExtendedPropertyDefinition(13825, MapiPropertyType.Integer)
    Dim rootFolderId As FolderId = New FolderId(WellKnownFolderName.Root)
    Dim folderView As FolderView = New FolderView(1000)
    folderView.Traversal = FolderTraversal.Shallow

    Dim searchFilter1 As SearchFilter = New SearchFilter.IsEqualTo(allFoldersType, "2")
    Dim searchFilter2 As SearchFilter = New SearchFilter.IsEqualTo(FolderSchema.DisplayName, "allitems")

    Dim searchFilterCollection1 As SearchFilter.SearchFilterCollection = New SearchFilter.SearchFilterCollection(LogicalOperator.[And])
    searchFilterCollection1.Add(searchFilter1)
    searchFilterCollection1.Add(searchFilter2)

    Dim FoldersResults As FindFoldersResults = service.FindFolders(rootFolderId, searchFilterCollection1, folderView)

    Dim allItemsFolder As Folder = Nothing

    If FoldersResults.Folders.Count > 0 Then allItemsFolder = FoldersResults.Folders(0)
    '========================================================================================================================

    Dim searchFilterCollection As List(Of SearchFilter) = New List(Of SearchFilter)()

    Dim searchdate As DateTime = Me.DateTimePicker1.Value.ToShortDateString
    ' Dim searchdate As DateTime = Date.Today

    ' Setup filter for date required to search within
    Dim greaterthanfilter As SearchFilter = New SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, searchdate)
    Dim lessthanfilter As SearchFilter = New SearchFilter.IsLessThan(ItemSchema.DateTimeReceived, searchdate.AddDays(1))

    ' Loads found emails into listview
    If service IsNot Nothing Then
        lstMsg.Items.Clear()

        ' Create a view with a page size of 50.
        Dim view As New ItemView(50)

        'Identify the Subject and DateTimeReceived properties to return.
        'Indicate that the base property will be the item identifier
        view.PropertySet = (New PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, ItemSchema.DateTimeReceived))

        ' Order the search results by the DateTimeReceived in descending order.
        view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending)

        ' Set the traversal to shallow. (Shallow is the default option; other options are Associated and SoftDeleted.)
        view.Traversal = ItemTraversal.Shallow

        ' I only want to retrieve certain emails by subject line if arrrived
        Dim LPSearchFiles As String() = {"Agent Name", "Agent Group", "Full Groups"} ' etc

        For i = 0 To LPSearchFiles.Count - 1
            ' Clear Searchfilters
            searchFilterCollection.Clear()

            ' Add date filter
            searchFilterCollection.Add(New SearchFilter.SearchFilterCollection(LogicalOperator.And, greaterthanfilter, lessthanfilter))

            If LPSearchFiles(i).Contains("Agent Name") Or LPSearchFiles(i).Contains("Agent Group") Then
                ' The subject lines in these contain dates this will still bring them back if the subject line contains the wording
                searchFilterCollection.Add(New SearchFilter.ContainsSubstring(ItemSchema.Subject, LPSearchFiles(i)))
            Else
                ' This brings back only if exact match
                searchFilterCollection.Add(New SearchFilter.IsEqualTo(ItemSchema.Subject, LPSearchFiles(i)))
            End If

            ' Create the search filter.
            Dim searchFilter As SearchFilter = New SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection.ToArray())

            ' Send the request to search the Inbox and get the results.
            Dim findResults As FindItemsResults(Of Item) = service.FindItems(allItemsFolder.Id, searchFilter, view)

            ' adds found emails to listview
            For Each item As Item In findResults
                Dim message As EmailMessage = EmailMessage.Bind(service, item.Id)
                Dim listitem As ListViewItem = New ListViewItem({message.DateTimeReceived.ToString(), message.From.Name.ToString() & "(" + message.From.Address.ToString() & ")", message.Subject, (If((message.HasAttachments), "Yes", "No")), message.Id.ToString()})
                lstMsg.Items.Add(listitem)
            Next
        Next i
    End If
End Sub

Private Function ValidateCertificate(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As SslPolicyErrors) As Boolean
    'Return True to force the certificate to be accepted.
    Return True
End Function