ExchangeService.FindFolders未返回任何文件夹

时间:2016-04-18 15:13:31

标签: c# .net exchange-server exchangewebservices

我有一个循环遍历邮箱中许多已定义文件夹的功能。每个文件夹都包含另一个名为" Complete"的文件夹。以下代码找到了这个"完成"文件夹并获取其FolderId

运行时,代码可以正常运行一段时间FindFoldersResults findFolderProcessed = service.FindFolders(folder.Id, new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "Complete"), view);不返回任何文件夹。单步执行代码,一切似乎都应该有效,但findFolderProcessed.Folders为空。

为什么它可以用于许多文件夹然后停止?

ServicePointManager.ServerCertificateValidationCallback =
    ((sender, certificate, chain, sslPolicyErrors) => true);

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
service.Credentials = new NetworkCredential("xxx", "xxx", "xxx");
service.AutodiscoverUrl("xxx@xxx.com");
service.Url = new Uri("https://xxx/ews/exchange.asmx");

FolderView view = new FolderView(int.MaxValue);
view.PropertySet = new PropertySet(BasePropertySet.IdOnly);
view.PropertySet.Add(FolderSchema.DisplayName);
view.Traversal = FolderTraversal.Deep;

SearchFilter[] parameters = new SearchFilter[3];
parameters[0] = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "x1");
parameters[1] = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "x2");
parameters[2] = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "x3");

SearchFilter.SearchFilterCollection filterCollection = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, parameters);

FindFoldersResults findFolder = service.FindFolders(new FolderId(WellKnownFolderName.Inbox, new Mailbox("xxx@xxx.com")), filterCollection, view);

foreach (Folder folder in findFolder.Folders)
{
    //FindFoldersResults tempResults = service.FindFolders(folder.Id, view);
    FindFoldersResults findFolderProcessed = service.FindFolders(folder.Id, new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "Complete"), view);
    FolderId ProcessedFolderID = findFolderProcessed.Folders[0].Id;

    //Other Processing
}

编辑:示例文件夹结构

x1
 -> Complete
x2
 -> Complete
x3
 -> Complete
...
xn
 -> Complete

大约有50个文件夹,结构完全相同。

1 个答案:

答案 0 :(得分:1)

所以问题是Thread正在返回重复的文件夹,并且循环尝试两次处理文件夹。所以它第一次正确地处理了文件夹,但是第二次它导致了这个问题。

我不知道为什么它会返回重复项,但为了修复它,我只是使用以下代码代替service.FindFolders重复findFolder

foreach (Folder folder in findFolder.Folders)

如果有人知道为什么文件夹会在最初的var folderCollection = findFolder.Folders.GroupBy(x => x.DisplayName).Select(g => g.First()); foreach (Folder folder in folderCollection) 电话中重复,请随时在下方发表评论。