首页 > 解决方案 > EWS:试图找到唯一的文件夹 ID

问题描述

我尝试使用 EWS API 编写解决方案来查找 Outlook 的唯一文件夹 ID。由于某种原因,代码无法正常工作,我无法弄清楚问题出在哪里。我没有使用 EWS API 的经验。

namespace ClassLibrary1
{
public class Class1
{

    static void Main(string[] args)
    {



        // Set server binding

        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
        service.UseDefaultCredentials = true;

        // Set Credentials
        service.Credentials = new WebCredentials("xxxxxxxxx", "xxxxxx");
        service.UseDefaultCredentials = true;

        // Set the URL 
        service.AutodiscoverUrl("xxxxx");

        // Set View

        FolderView view = new FolderView(100);
        view.PropertySet = new PropertySet(BasePropertySet.IdOnly);
        view.PropertySet.Add(FolderSchema.DisplayName);
        view.Traversal = FolderTraversal.Deep;
        FindFoldersResults findFolderResults = service.FindFolders(WellKnownFolderName.Root, view);

        // Find specific folder

        foreach (Folder f in findFolderResults)
        {

            // Show FolderId of the folder "test"
            if (f.DisplayName == "Test")
                Console.WriteLine(f.Id);
        }

    }
}

}

它说未处理异常,autodsicover 阻止了潜在的不安全重定向(..)

标签: c#visual-studio-2010exchangewebservices

解决方案


引发错误时,它提到您需要使用回调来允许重定向。沿线的东西

service.AutodiscoverUrl("xxxxx", ValidateRedirectionUrlCallback);
...
private static bool ValidateRedirectionUrlCallback(string url)
{
    // Validate the URL and return true to allow the redirection or false to prevent it.
    return true; //allow redirections
}

推荐阅读