Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How can I connect to an exchange server and read mail from a shared mailbox (one that is not my own "[email protected]").

Here is my code thus far:

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


               }    
                }

When I run this, I get an exception thrown that says that that "The SMTP address has no mailbox associated with it " during:

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

What am I missing? I feel like I am almost there and that this should work according to the EWS Managed API 2.0 documentation, but I

like image 616
Dillon Willis Avatar asked Feb 18 '16 11:02

Dillon Willis


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

C is an imperative, procedural language in the ALGOL tradition. It has a static type system. In C, all executable code is contained within subroutines (also called "functions", though not in the sense of functional programming).

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


1 Answers

You should just be using the FolderId overload to specify the Mailbox you want to access. eg if your shared Mailbox was called [email protected] then use

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

Also don't use

ItemView itemView = new ItemView(int.MaxValue);

This won't work as Exchange will restrict the Maximum amount of items returned due to throttling. Always paget the result for findItems and findfolders see http://blogs.msdn.com/b/exchangedev/archive/2010/03/12/throttling-policies-and-the-ewsfindcountlimit.aspx

Cheers Glen

like image 120
Glen Scales Avatar answered Sep 18 '22 21:09

Glen Scales