none
Attempting to access office 365 exchange via web service RRS feed

  • Question

  • I am attempting to read some emails from office 365. 

    I have been through everything that I can think of and it still seems to fail - to the point where Im wondering if there are special permissions or if its possible that someone has disabled the service and not realised!

    So the code I have so fair is fairly basic :

    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
    
    service.Credentials = new WebCredentials("MyAdUn", "MyADPw", "DOMAIN");
    service.UseDefaultCredentials = false;
    service.AutodiscoverUrl("me@workemailaddress.com", RedirectionUrlValidationCallback);
    FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));

    The plan would then be to iterate over the lift of results.

    The problem is that when I run the above (with the credentials changed for the actual values) it seems to sit trying several endpoints for the service (in the AutodiscoverUrl call) and then finally errors with "The Autodiscover service couldn't be located"

    Im looking for someone to either point at the code and go... "Oi dummy, look at that dumb thing you did" or to let me know if there are special permissions needed - Ive done this years ago for an on site exchange server and had none of these problems!

    Tuesday, July 25, 2017 7:11 PM

All replies

  • Your credential format isn't correct for Office365 the download level credential format won't work you need to use the users UPN which you can find easily as the username in the Office365 console and use the username and password overload for Webcredentials eg

    service.Credentials = new WebCredentials("user@somedomain.onmicrosoft.com", "password");

    the account your using here should have its own mailbox Mailbox and Office365 Licence

    Also you need to understand that whatever credentials you using in the above won't have access to the any mailbox other then their own by default unless its been explicitly granted. (eg using Add-MailboxPermission). If you want to access a Mailbox other than that of the credentials your using then you should use the Mailbox overload of the FolderId class to specify the Mailbox you want to access. eg

    FolderId folderToAccess = new FolderId(WellKnownFolderName.Inbox, "me@workemailaddress.com");
    FindItemsResults<Item> findResults = service.FindItems(folderToAccess, new ItemView(10));

    Cheers
    Glen

    Wednesday, July 26, 2017 4:56 AM