2

非常感谢任何帮助、链接或想法。谢谢!

总体目标

如何在 Exchange 2013 中查询特定折叠以读取每条消息的正文,以便我可以从自动警报中收集信息并写入 csv 文件?

具体问题我想不通

如何查询特定文件夹?

问题

我只能找到如何搜索收件箱的示例。我需要更改该语句以说出特定文件夹。

问题所在的当前代码部分

 try
            {
                //read the subject line of the 10 most recently recieved emails in the inbox
                FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
                foreach (Item item in findResults.Items)
                {
                    Console.WriteLine(item.Subject);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
                Console.ReadLine();
            }
            Console.ReadLine();

问题线

FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
4

2 回答 2

2

只是为了后代,这里是在 EWS 2010 中按特定名称查找文件夹的代码(假设仅存在一个具有此名称的文件夹):

Public Function GetMailFolderByName(ByVal folderName As String) As Folder Implements IMailboxExchange.GetMailFolderByName
    Me.ThrowIfNoConnection()
    Dim folderView As FolderView = New FolderView(1)
    folderView.PropertySet = New PropertySet(BasePropertySet.IdOnly)
    folderView.PropertySet.Add(FolderSchema.DisplayName)
    folderView.Traversal = FolderTraversal.Deep
    Dim searchFilterByFolderName As SearchFilter = New SearchFilter.ContainsSubstring(FolderSchema.DisplayName, folderName)
    Dim results As FindFoldersResults = Me._exchangeService.FindFolders(WellKnownFolderName.Root, searchFilterByFolderName, folderView)
    If results Is Nothing OrElse results.Folders Is Nothing OrElse results.Folders.Count = 0 Then
        Return Nothing
    End If
    Return results.Folders(0)
End Function

重要提示: 是的,我知道它在 VB.NET 中,但应该可以翻译成 C#。它也是 EWS 2010 而不是 2013,但它们可能没有太大变化。

于 2013-08-13T11:16:01.800 回答
2
try
{
    //create searchable object called rootfolder and set root to equal Inbox
    Folder rootfolder = Folder.Bind(service, WellKnownFolderName.Inbox);
    //foreach child folder inside Ibox
    foreach (Folder folder in rootfolder.FindFolders(new FolderView(100)))
    {
        //if the child folder is named hey
        if (folder.DisplayName.Contains("hey"))
        {
            //create searchable object of child of root but now is root to be searched within folder and set it as root folder
            Folder childfolder = Folder.Bind(service, folder.Id);
            //foreach childfolder to new root
            foreach (Folder cfolder in childfolder.FindFolders(new FolderView(100)))
            {
                //if child folder is blah then search items
                if (cfolder.DisplayName.Contains("blah"))
                {
                    //create searchable object of blah folder and set it as root folder
                    FindItemsResults<Item> blah = service.FindItems(cfolder.Id, new ItemView(10));
                    //this will load all the extra properites for each email (SUCH AS BODY)
                    service.LoadPropertiesForItems(from Item item in blah select item, PropertySet.FirstClassProperties);
                    foreach (Item item in blah.Items)
                    {
                       //Console.WriteLine(item.DateTimeReceived + "\n\t" + item.Body);
                        Console.WriteLine(item.Subject);
                        Console.WriteLine("\t"+item.DateTimeReceived);
                        Console.WriteLine("\t" + item.Body.Text.ToString());
                    }
                }
            }
        }
    }


}
catch (Exception e)
{
    Console.WriteLine(e.Message.ToString());
}
Console.ReadLine();
于 2013-07-08T21:05:27.280 回答