EWS如何在自己的邮箱中创建Searchfolder?

时间:2019-03-20 12:28:17

标签: c# exchangewebservices

我使用以下代码创建SearchFolder,但是当它到达“保存”行时,抛出以下错误:

  

与文件夹ID关联的电子邮件地址与   您正在使用的邮箱。

private SearchFolder CreateSearchFolder( string email, SearchFilter filter)
{
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
    service.Credentials = new WebCredentials("mailboxworker", "password");
    service.AutodiscoverUrl(email);

    FolderId folderId = new FolderId(WellKnownFolderName.Inbox, new Mailbox(email));

    FolderId searchFolderId = new FolderId(WellKnownFolderName.SearchFolders, new Mailbox(email));

    // Create the folder.
    SearchFolder searchFolder = new SearchFolder(service);
    searchFolder.DisplayName = "Folder of " + email;

    searchFolder.SearchParameters.SearchFilter = filter;
    // Set the folder to search.
    searchFolder.SearchParameters.RootFolderIds.Add(folderId);   

    // Set the search traversal. Deep will search all subfolders.
    searchFolder.SearchParameters.Traversal = SearchFolderTraversal.Deep;
    // Call Save to make the EWS call to create the folder.
    searchFolder.Save(searchFolderId);

    return searchFolder;
}

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

  

与文件夹ID关联的邮箱不匹配

一直以来,我都使用Microsoft.Exchange.WebServices.Data WellKnownFolderName枚举而不是字符串folderId对其进行了修复

这是MSDN上的一个有效示例:Create a search folder by using the EWS Managed API

  

此示例假定ExchangeService对象已使用Credentials和Url属性中的有效值初始化。

using Microsoft.Exchange.WebServices.Data;

static void CreateSearchFolder(string email)
{
    // Create the folder.
    SearchFolder searchFolder = new SearchFolder(service);
    searchFolder.DisplayName = "From Developer";
    // Create a search filter to express the criteria for the folder.
    EmailAddress developer= new EmailAddress("Jeremy@stackoverflow.com");
    SearchFilter.IsEqualTo fromManagerFilter = new SearchFilter.IsEqualTo(EmailMessageSchema.Sender, developer);

    // Set the search filter.
    searchFolder.SearchParameters.SearchFilter = fromManagerFilter;
    // Set the folder to search.
    searchFolder.SearchParameters.RootFolderIds.Add(WellKnownFolderName.Inbox);
    // Set the search traversal. Deep will search all subfolders.
    searchFolder.SearchParameters.Traversal = SearchFolderTraversal.Deep;
    // Call Save to make the EWS call to create the folder.
    searchFolder.Save(WellKnownFolderName.SearchFolders);
}

这是MSDN Creating search folders by using the EWS Managed API 2.0

上的另一个示例