我正在尝试使用Microsoft.Exchange.WebServices Managed API和基于this FindFolder() example的代码从Exchange帐户枚举仅限电子邮件文件夹。
以下代码会过滤所有空文件夹,以及Task
,Search
,Contacts
和Calendar
文件夹,但仍会返回FreeBusy Data
,Recoverable Items
和Deletions
文件夹。
有没有办法可以从返回的文件夹中过滤掉这些文件,而不是通过名字上的字符串匹配?事实上,还有一种过滤Sent Items
和Deleted Items
的方法吗? (虽然因为这些不能重命名(而且我对它们的内容有了更好的了解!),我不再担心只是按名称过滤掉这些内容)
Dim moreFoldersToReturn As Boolean = True
Const FOLDER_PAGE_SIZE As Integer = 10
Dim folderOffset As Integer = 0
While moreFoldersToReturn
' Create a view with a page size of x
Dim view As New FolderView(FOLDER_PAGE_SIZE, folderOffset)
' Identify the properties to return in the results set.
view.PropertySet = New PropertySet(BasePropertySet.IdOnly)
view.PropertySet.Add(FolderSchema.DisplayName)
' Create an extended property definition for the PidTagAttributeHidden property.
Dim isHiddenProp As ExtendedPropertyDefinition = New ExtendedPropertyDefinition(&H10F4, MapiPropertyType.Boolean)
Dim searchFiltersCollection As List(Of SearchFilter) = New List(Of SearchFilter)
searchFiltersCollection.Add(New SearchFilter.IsEqualTo(isHiddenProp, False)) 'filter hidden folders
searchFiltersCollection.Add(New SearchFilter.IsGreaterThan(FolderSchema.TotalCount, 0)) 'filter empty folders
Dim searchFilterToUse As SearchFilter = New SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFiltersCollection.ToArray())
' Unlike FindItem searches, folder searches can be deep traversals.
view.Traversal = FolderTraversal.Deep
' Send the request to search the mailbox and get the results.
Dim results As FindFoldersResults = Me.exchangeServer.connection.FindFolders(WellKnownFolderName.Root, searchFilterToUse, view)
' Process each item.
Dim folder As Folder
For Each folder In results.Folders
If Not TypeOf folder Is SearchFolder AndAlso
Not TypeOf folder Is ContactsFolder AndAlso
Not TypeOf folder Is TasksFolder AndAlso
Not TypeOf folder Is CalendarFolder Then
Debug.WriteLine("Folder: " & folder.DisplayName)
foldersToReturn.Add(folder)
End If
Next
folderOffset += FOLDER_PAGE_SIZE
moreFoldersToReturn = results.MoreAvailable
End While
提供以下输出:
Folder: Freebusy Data
Folder: Recoverable Items
Folder: Deletions
Folder: Deleted Items
Folder: Inbox
Folder: Sent Items
Folder: TestEmailFolder
答案 0 :(得分:3)
我建议你从MsgFolderRoot而不是Root开始你的搜索,这意味着你不会得到像可恢复的项目,FreeBusy数据文件夹等返回的Non_IPM文件夹。您还可以使用搜索文件夹并将返回的文件夹限制为具有IPF的FolderClass的文件夹。注意例如
SearchFilter sfFolder = new SearchFilter.IsEqualTo(FolderSchema.FolderClass,"IPF.Note");
FindFoldersResults ffResults = service.FindFolders(WellKnownFolderName.MsgFolderRoot, sfFolder, new FolderView(1000));
但是这可能会排除一些文件夹没有设置文件夹类或使用像IPF.Note.Microsoft.Conversation这样的子类。因此,如果您需要处理这些文件夹,过滤可能仍然是最佳选择。