【问题标题】:VB.Net EWS Howto start search for mail items from a specified Sub folder and look in all sub foldersVB.Net EWS 如何开始从指定的子文件夹中搜索邮件项目并查看所有子文件夹
【发布时间】:2019-11-25 19:13:32
【问题描述】:

使用 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

【问题讨论】:

  • 好的——这就是你到目前为止所做的。有问题吗?它有效吗?如果不起作用,错误或意外输出在哪里?
  • 不,如果我移除 2 个过滤器 searchFilterCollection.Add(New SearchFilter.IsEqualTo(FolderSchema.DisplayName, "My Folder")) searchFilterCollection.Add(New SearchFilter.ContainsSubstring(ItemSchema.主题,“测试主题”))然后它从主收件箱中带回当天的电子邮件,我想以某种方式获取它,以便我可以在收件箱及其子文件夹中搜索给定主题的文件夹,但我无法得到它从指定的子文件夹开始
  • 我找到了一个不理想但确实有效的解决方法,在此行之前 Dim findResults As FindItemsResults(Of Item) = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view),我有一个函数调用为我获取文件夹名称的文件夹详细信息,在这一行中用作 myfolder.id 而不是 wellknown 文件夹,但您必须获取每个文件夹的 id,我想要的是设置 staruing 文件夹以搜索到某事和然后只搜索该文件夹和所有子文件夹。
  • 我有一个问题,一些电子邮件登陆“其他”邮箱并且没有集中,我如何在那个邮箱中搜索,我尝试搜索其他文件夹并尝试了我找到的其他代码它找到了“Allitems”文件夹,但它仍然没有显示任何位于“Other”中的电子邮件
  • 您可以通过此链接将该信息添加到问题中:stackoverflow.com/posts/58940977/edit - 这将改善您的问题并提高您获得答案的机会。

标签: vb.net exchangewebservices


【解决方案1】:

下面的代码允许我搜索覆盖“其他”文件夹的所有项目文件夹,并仅按日期检索特定电子邮件

注意:在这里找到代码:Exchange Web Services (EWS) FindItems within All Folders

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多