【问题标题】:C# EWS Managed API: How to access shared mailboxes but not my own inboxC# EWS 托管 API:如何访问共享邮箱而不是我自己的收件箱
【发布时间】:2016-05-30 13:38:44
【问题描述】:

如何连接到 Exchange 服务器并从共享邮箱(不是我自己的“myname@mycompany.com”)读取邮件。

到目前为止,这是我的代码:

//Create a service
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
        //Autodiscover end point
        service.AutodiscoverUrl("someaddress@mycompany.com");


        FindFoldersResults folderSearchResults = service.FindFolders(WellKnownFolderName.Inbox, new FolderView(int.MaxValue));

        Microsoft.Exchange.WebServices.Data.Folder exchangeMailbox = folderSearchResults.Folders.ToList().Find(
            f => f.DisplayName.Equals("NameOfSharedMailboxIwant", StringComparison.CurrentCultureIgnoreCase));

        //Set the number of items we can deal with at anyone time.
        ItemView itemView = new ItemView(int.MaxValue);

        foreach (Microsoft.Exchange.WebServices.Data.Folder folderFromSearchResults in folderSearchResults.Folders)
        {
            if (folderFromSearchResults.DisplayName.Equals("NameOfSharedMailboxIWant", StringComparison.OrdinalIgnoreCase))
            {
                Microsoft.Exchange.WebServices.Data.Folder boundFolder = 
                        Microsoft.Exchange.WebServices.Data.Folder.Bind(service, folderFromSearchResults.Id);

                SearchFilter unreadSearchFilter =
                    new SearchFilter.SearchFilterCollection(
                        LogicalOperator.And, new SearchFilter.IsEqualTo(
                            EmailMessageSchema.IsRead, false));

                //Find the unread messages in the email folder.
                FindItemsResults<Item> unreadMessages = boundFolder.FindItems(unreadSearchFilter, itemView);

                foreach (EmailMessage message in unreadMessages)
                {
                    message.Load();

                   Console.WriteLine(message.Subject);


               }    
                }

当我运行它时,我收到一个异常,提示“SMTP 地址没有与之关联的邮箱”:

 Microsoft.Exchange.WebServices.Data.Folder exchangeMailbox = folderSearchResults.Folders.ToList().Find(
            f => f.DisplayName.Equals("BA", StringComparison.CurrentCultureIgnoreCase));

我错过了什么?我觉得我快到了,这应该根据 EWS Managed API 2.0 文档工作,但我

【问题讨论】:

    标签: c# .net exchangewebservices


    【解决方案1】:

    您应该只使用 FolderId 重载来指定您要访问的邮箱。例如,如果您的共享邮箱名为 Shared@domain.com,则使用

    FolderId SharedMailbox = new FolderId(WellKnownFolderName.Inbox,"Shared@domain.com");
    ItemView itemView = new ItemView(1000);
    service.FindItems(SharedMailbox,itemView);
    

    也不要使用

    ItemView itemView = new ItemView(int.MaxValue);

    这不起作用,因为 Exchange 将限制由于限制而返回的最大项目数量。始终对 findItems 和 findfolders 的结果进行分页,请参阅 http://blogs.msdn.com/b/exchangedev/archive/2010/03/12/throttling-policies-and-the-ewsfindcountlimit.aspx

    干杯 格伦

    【讨论】:

    • 有没有办法枚举您所属的所有共享邮箱?
    • 我在这里发布了一个后续问题:stackoverflow.com/questions/38881919/…
    • 此答案不适用于没有众所周知的文件夹名称。
    • 如果我要使用的共享邮箱没有登录怎么办?我该如何使用它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 2011-08-04
    • 1970-01-01
    相关资源
    最近更新 更多