通过 EWS(Exchange WebServices)获取所有邮箱 - 不是我自己的邮箱,也包括共享邮箱和群组邮箱

发布于 2025-01-05 12:31:07 字数 463 浏览 3 评论 0原文

谁能给我提供一个 .NET (C# / VB) 示例,说明如何获取我有权访问的所有邮箱?

我只能通过 EWS 获取自己的邮箱 - 而不能通过 Outlook 访问所有其他邮箱。

我没有这些邮箱的名称和 ID,但是否可以检索我可以查看的所有邮箱(包括我自己的邮箱),就像在 Outlook 中一样?

我正在使用自动发现来获取我的邮箱,如下所示: service.AutodiscoverUrl("[电子邮件受保护]") - 这可能只会获取我自己的邮箱,而不是其他邮箱?

请帮忙!?

Can anyone provide me with a .NET (C# / VB) sample of how to get all mailboxes that I have access to ?

I have only been able to get my OWN mailbox via EWS - not ALL THE OTHER mailboxes that I also have access to through Outlook.

I don't have the names nor the id's of these mailboxes but isn't it possible to retrieve ALL mailboxes - including my own - that I am allowed to see - just as I can in Outlook ?

I am using Autodiscover to get my mailbox like this: service.AutodiscoverUrl("[email protected]") - this will perhaps only get my own mailbox and not all the rest?

Please help !?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手长情犹 2025-01-12 12:31:07

我解决这个问题的方法是将有问题的组邮箱定义为“邮箱”对象,然后获取特定文件夹的FolderID。

  1. 定义邮箱对象

    邮箱 gpmailbox = new 邮箱("[电子邮件受保护]");
    
  2. 获取FolderID(在本例中为收件箱)

    FolderId gpInbox = new FolderId(WellKnownFolderName.Inbox, gpmailbox);
    
  3. 在普通代码中使用FolderID(在本例中我获取了100 条消息)

    ItemView 视图 = new ItemView(100);        
    FindItemsResults<项目>结果 = hookToServer.FindItems(new FolderId(WellKnownFolderName.Inbox, gpmailbox), view);
    

关键是获取您需要的文件夹的FolderID。希望这有帮助。

编辑:我也没有注意到我的对象“hookToServer”只是 ExchangeService 对象。我是这样定义的:

        ExchangeService hookToServer = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
        hookToServer.UseDefaultCredentials = true;
        hookToServer.Url = new Uri("TheExchangeServer")

我也用这个作为参考:
EWS 2007 群组邮箱指南

The way I got around this was to define the group mailbox in question as a "mailbox" object and then obtain the FolderID for the particular folder.

  1. Define mailbox object

    Mailbox gpmailbox = new Mailbox("[email protected]");
    
  2. Get the FolderID (in this case, the Inbox)

    FolderId gpInbox = new FolderId(WellKnownFolderName.Inbox, gpmailbox);
    
  3. Use FolderID in your normal code (in this case I'm obtaining 100 messages)

    ItemView view = new ItemView(100);        
    FindItemsResults<Item> results = hookToServer.FindItems(new FolderId(WellKnownFolderName.Inbox, gpmailbox), view);
    

The key is to grab the FolderID of the folder you need. Hope this helps.

Edit: I also failed to note that my object "hookToServer" is simply the ExchangeService object. Here's how I defined it:

        ExchangeService hookToServer = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
        hookToServer.UseDefaultCredentials = true;
        hookToServer.Url = new Uri("TheExchangeServer")

I also used this as reference:
EWS 2007 Group Mailbox Guide

凝望流年 2025-01-12 12:31:07

您可以通过 使用自动发现获取用户设置,这是一项与使用 AutodiscoverUrl 方法的服务完全独立的服务。

您需要的设置名称是 AlternateMailboxes,这将提供您有权访问的所有“其他”邮箱的集合。然后,您可以添加用户的默认邮箱以获得完整列表。

在 c# 中:

using Microsoft.Exchange.WebServices.Autodiscover;  // from nuget package "Microsoft.Exchange.WebServices"

    internal List<string> GetAccessibleMailboxes()
    {
        AutodiscoverService autodiscoverService = new AutodiscoverService("outlook.office365.com");
        autodiscoverService.Credentials = networkCredential;
        var userSmtpAddress = networkCredential.UserName;

        GetUserSettingsResponse userresponse = autodiscoverService.GetUserSettings(
            userSmtpAddress,
            UserSettingName.AlternateMailboxes);

        var alternateMailboxCollection = (AlternateMailboxCollection)userresponse.Settings[UserSettingName.AlternateMailboxes];
        var smtpAddressList = alternateMailboxCollection.Entries.ToList().Select(a => a.SmtpAddress).ToList();

        smtpAddressList.Add(userSmtpAddress);

        return smtpAddressList;
    }

You can do this by Using Autodiscover to get user settings, this is a completely separate service to the one with the AutodiscoverUrl method.

The setting name you need is AlternateMailboxes, this will give a collection of all the 'other' mailboxes you have access to. You might then add the user's default mailbox to get a complete list.

In c#:

using Microsoft.Exchange.WebServices.Autodiscover;  // from nuget package "Microsoft.Exchange.WebServices"

    internal List<string> GetAccessibleMailboxes()
    {
        AutodiscoverService autodiscoverService = new AutodiscoverService("outlook.office365.com");
        autodiscoverService.Credentials = networkCredential;
        var userSmtpAddress = networkCredential.UserName;

        GetUserSettingsResponse userresponse = autodiscoverService.GetUserSettings(
            userSmtpAddress,
            UserSettingName.AlternateMailboxes);

        var alternateMailboxCollection = (AlternateMailboxCollection)userresponse.Settings[UserSettingName.AlternateMailboxes];
        var smtpAddressList = alternateMailboxCollection.Entries.ToList().Select(a => a.SmtpAddress).ToList();

        smtpAddressList.Add(userSmtpAddress);

        return smtpAddressList;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文