【问题标题】:Trying to find unique id of folder in EWS C# API试图在 EWS C# API 中找到文件夹的唯一 ID
【发布时间】:2019-05-25 07:38:08
【问题描述】:

我正在尝试在 Outlook 中查找文件夹的唯一 ID。

由于某种原因,AutoDiscoverUrl 方法不断出错,但我不知道为什么。我已经在 StackOverflow 上尝试了所有可能的解决方案。

我正在尝试使用 C# 在命令行程序中运行它,并已对代码进行了注释/记录。我已经使用了其他人的示例来说明如何执行此操作,但它不起作用。

static void Main(string[] args)
{
    // Set server binding
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
    service.UseDefaultCredentials = true;

    // Set Credentials
    service.Credentials = new WebCredentials("xxxx", "xxxxx", "xxxx");
    service.UseDefaultCredentials = true;

    service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");

    // Set the URL 
    service.AutodiscoverUrl("xxxx", Callback);

    // Error Tracing
    service.TraceEnabled = true;

    // Redirect callback

    // 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);
        }
    }
}

private static bool Callback(string redirectionUrl)
{
    bool result = false;
    var redirectionUri = new Uri(redirectionUrl);
    if (redirectionUri.Scheme == "https")
    {
        result = true;
    }
    return result;
}

【问题讨论】:

  • 您遇到的错误是什么?
  • 另外这是office365的吗?如果是这样,您可以考虑使用 Microsoft 图形 API 作为替代解决方案...docs

标签: c# visual-studio-2010 outlook exchangewebservices


【解决方案1】:

您可以使用以下代码找到文件夹的唯一 ID:

ExchangeService Service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
Service.UseDefaultCredentials = false;
Service.Credentials = new WebCredentials("yourUserID", "yourPassword");

Mailbox ProdSupportMailbox = new Mailbox("ProdSupport@company.com");
Service.AutodiscoverUrl("ProdSupport@company.com");

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

FindFoldersResults findFolderResults = server.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);
    }
}

更多信息,请参考以下链接:

Exchange Web Service FolderId for a not well known folder name

Microsoft Exchange Web Services reading my local Outlook folders INSTEAD of another address

【讨论】:

  • 我可以在 main 方法中运行它吗?
  • 是的,你可以在 main 方法中运行它。
  • 完成了,您知道如何将代码更改为,而不是查找特定文件夹,而是查找此邮箱中的所有文件夹吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-11
  • 1970-01-01
  • 2017-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多