Search specific folder on Exchange Server

时间:2017-05-16 09:14:59

标签: c# api exchange-server exchange-server-2010

I have a block of code that currently reads all emails on an exchange server from the general "Inbox" folder. The line below is what is accessing that particular folder

 FindItemsResults<Item> findResults = exchange.FindItems(WellKnownFolderName.Inbox, new ItemView(250));

Question is it possible to adapt this code so that I can search a user created folder instead of the generic inbox? for example I have created a folder called "Test Folder", and I want to be able to read all emails in here.

I have tried adding a Search Filter but this returns no results at present:

 SearchFilter searchFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "Test Folder");

 FindItemsResults<Item> findResults = exchange.FindItems(WellKnownFolderName.Inbox, searchFilter, new ItemView(250));

1 个答案:

答案 0 :(得分:1)

您需要首先找到用户创建的文件夹的FolderId,然后您可以使用FolderId在服务类的FindItem方法中使用它,或者在文件夹本身上使用FindItem方法。例如,要查找用户创建的文件夹,您可以将其转换为路径并搜索该路径

        internal static Folder GetFolderFromPath(ExchangeService service,String MailboxName,String FolderPath)
{
    FolderId folderid = new  FolderId(WellKnownFolderName.MsgFolderRoot,MailboxName);   
    Folder tfTargetFolder = Folder.Bind(service,folderid);
    PropertySet psPropset = new PropertySet(BasePropertySet.FirstClassProperties);
    String[] fldArray = FolderPath.Split('\\'); 
    for (Int32 lint = 1; lint < fldArray.Length; lint++) { 
        FolderView fvFolderView = new FolderView(1);
        fvFolderView.PropertySet = psPropset;
        SearchFilter  SfSearchFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName,fldArray[lint]); 
        FindFoldersResults findFolderResults = service.FindFolders(tfTargetFolder.Id,SfSearchFilter,fvFolderView); 
        if (findFolderResults.TotalCount > 0){ 
        foreach(Folder folder in findFolderResults.Folders){ 
            tfTargetFolder = folder;                
            } 
        } 
        else{ 
            tfTargetFolder = null;  
            break;  
        }     
    }
    if (tfTargetFolder != null)
    {
        return tfTargetFolder;
    }
    else
    {
        throw new Exception("Folder Not found");
    }
}

因此,如果文件夹位于收件箱内,您可以使用

GetFolderFromPath(service,“mailbox @ domaim.com”,“\ Inbox \ folder”),然后返回该文件夹。