Search code examples
c#emailserviceexchangewebservices

Service AutoDiscoverUrl On a Third Party Address


I am using an example on StackOverFlow as follow:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

//service.Credentials = new NetworkCredential( "{Active Directory ID}", "{Password}", "{Domain Name}" );

service.AutodiscoverUrl( "First.Last@MyCompany.com" );

FindItemsResults<Item> findResults = service.FindItems(
   WellKnownFolderName.Inbox,
   new ItemView( 10 ) 
);

foreach ( Item item in findResults.Items )
{
   Console.WriteLine( item.Subject );
}

Currently, it retrieves the mail from my logged in AD account regardless of the email address entered in service.AutodiscoverUrl.

Do I need to fill in service.Credentials in order to access the mailbox of another user?

Thank you.


Solution

  • Autodiscover just discovers the best endpoint to use for EWS based on the user but doesn't affect the Mailbox your trying to access. For Credentials you should use the user that has the rights necessary to access whatever your trying to access, if you want to access a Mailbox other then the credentials you using you need to use the Mailbox override of the folder id class eg so in your example

                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
    
                //service.Credentials = new NetworkCredential( "{Active Directory ID}", "{Password}", "{Domain Name}" );
    
                service.AutodiscoverUrl("First.Last@MyCompany.com");
                FolderId FolderToAccess = new FolderId(WellKnownFolderName.Inbox, "First.Last@MyCompany.com");
    
                FindItemsResults<Item> findResults = service.FindItems(
                   FolderToAccess,
                   new ItemView(10)
                );
    
                foreach (Item item in findResults.Items)
                {
                    Console.WriteLine(item.Subject);
                }