2016-02-18 17 views
8

Làm cách nào để kết nối với máy chủ trao đổi và đọc thư từ hộp thư dùng chung (hộp thư không phải là "[email protected]" của riêng tôi).C# EWS Managed API: Cách truy cập hộp thư dùng chung nhưng không phải hộp thư riêng của tôi

Đây là mã của tôi vậy, đến nay:

//Create a service 
     ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); 
     //Autodiscover end point 
     service.AutodiscoverUrl("[email protected]"); 


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


       }  
       } 

Khi tôi chạy này, tôi nhận được một ngoại lệ ném mà nói rằng "Các địa chỉ SMTP không có hộp thư liên kết với nó" trong:

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

Tôi đang thiếu gì? Tôi cảm thấy như tôi gần như ở đó và điều này sẽ hoạt động theo tài liệu EWS Managed API 2.0, nhưng tôi

Trả lời

12

Bạn chỉ nên sử dụng quá tải FolderId để chỉ định Hộp thư bạn muốn truy cập. ví dụ như nếu Mailbox chia sẻ của bạn được gọi là [email protected] sau đó sử dụng

FolderId SharedMailbox = new FolderId(WellKnownFolderName.Inbox,"[email protected]"); 
ItemView itemView = new ItemView(1000); 
service.FindItems(SharedMailbox,itemView); 

Cũng không sử dụng

ItemView itemView = new ItemView(int.MaxValue);

này sẽ không làm việc như Exchange sẽ hạn chế số lượng tối đa các mặt hàng trở lại do điều chỉnh. Luôn Paget kết quả cho findItems và findfolders thấy http://blogs.msdn.com/b/exchangedev/archive/2010/03/12/throttling-policies-and-the-ewsfindcountlimit.aspx

Cheers Glen

+0

Có cách nào để liệt kê tất cả các hộp thư chung bạn là một phần của? – Alexandru

+0

Tôi đã đăng câu hỏi tiếp theo tại đây: http://stackoverflow.com/questions/38881919/enumerating-shared-mailbox-names-you-are-able-to-access-using-ews-managed-api – Alexandru

+1

Điều này câu trả lời không phù hợp cho không có tên thư mục nổi tiếng nào. – Myzifer

Các vấn đề liên quan