簡體   English   中英

C#EWS托管API:如何訪問共享郵箱,但不訪問我自己的收件箱

[英]C# EWS Managed API: How to access shared mailboxes but not my own inbox

如何連接到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文檔工作,但我

您應該只使用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

干杯格倫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM