Search code examples
c#oauth-2.0office365exchange-serveroffice365api

Basic Authentication is not more working while reading mail from Outlook Inbox


There was a legacy application running behind the scenes to pull new mails from a mailbox to database. The code looks like this

ExchangeService exchangeService = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
exchangeService.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
                 
exchangeService.Credentials = new NetworkCredential("mail@domain.com","mailpassword", "");
PropertySet ItemPropertySet = new PropertySet(BasePropertySet.FirstClassProperties);
ItemPropertySet.RequestedBodyType = BodyType.Text;
ItemView itemView = new ItemView(10);
itemView.PropertySet = ItemPropertySet;

FindItemsResults<Item> Emails = exchangeService.FindItems(WellKnownFolderName.Inbox,
                    new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter[]{
                    new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false)
                    }),
                    itemView);

This code was working fine But in last few days i was getting error while execute FindItems

11/10/2022 03:55:00 - ERROR WITH INBOUND EMAIL TASK TASK: The request failed. The remote server returned an error: (401) Unauthorized.
Microsoft.Exchange.WebServices
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request) in \\REDMOND\EXCHANGE\BUILD\E15\15.00.0913.015\SOURCES\sources\dev\EwsManagedApi\src\EwsManagedApi\Core\Requests\ServiceRequestBase.cs:line 831
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request) in \\REDMOND\EXCHANGE\BUILD\E15\15.00.0913.015\SOURCES\sources\dev\EwsManagedApi\src\EwsManagedApi\Core\Requests\ServiceRequestBase.cs:line 724
   at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute() in \\REDMOND\EXCHANGE\BUILD\E15\15.00.0913.015\SOURCES\sources\dev\EwsManagedApi\src\EwsManagedApi\Core\Requests\MultiResponseServiceRequest.cs:line 157
   at Microsoft.Exchange.WebServices.Data.ExchangeService.FindItems[TItem](IEnumerable`1 parentFolderIds, SearchFilter searchFilter, String queryString, ViewBase view, Grouping groupBy, ServiceErrorHandling errorHandlingMode) in \\REDMOND\EXCHANGE\BUILD\E15\15.00.0913.015\SOURCES\sources\dev\EwsManagedApi\src\EwsManagedApi\Core\ExchangeService.cs:line 832
   at Microsoft.Exchange.WebServices.Data.ExchangeService.FindItems(FolderId parentFolderId, SearchFilter searchFilter, ViewBase view) in \\REDMOND\EXCHANGE\BUILD\E15\15.00.0913.015\SOURCES\sources\dev\EwsManagedApi\src\EwsManagedApi\Core\ExchangeService.cs:line 937
   at Microsoft.Exchange.WebServices.Data.ExchangeService.FindItems(WellKnownFolderName parentFolderName, SearchFilter searchFilter, ViewBase view) in \\REDMOND\EXCHANGE\BUILD\E15\15.00.0913.015\SOURCES\sources\dev\EwsManagedApi\src\EwsManagedApi\Core\ExchangeService.cs:line 990

I came to see the official announcememnt from Mirosoft about Deprecation of Basic Authentication (Basic Auth) in Exchange Online

https://learn.microsoft.com/en-us/lifecycle/announcements/basic-auth-deprecation-exchange-online

So how can we update this code to read the emails again?


Solution

  • So how can we update this code to read the emails again?

    You have the following options:

    1. If you need more time to update your code, you can request a one-time re-enablement. This will re-active basic authentication until December 2022.

    2. Rewrite your application to use OAuth instead of Basic Authentication. You can find Microsoft's official step-by step instructions here, including sample code:

    3. Do note, though, that EWS will not get feature updates, so you might want to use this opportunity to migrate your application to MS Graph: