Free Support Forum -

"401 Unauthorized" when connecting to Outlook 365 with OAuth2

I successfully retrieved a token, but when trying to get a list of messages, the Aspose library (Aspose.Email version 20.6) always tells me I’m unauthorized. I’ve tried this with multiple accounts and verified the credentials. This is the exception with stack trace:

System.Net.WebException: The request failed with HTTP status 401: Unauthorized.
   at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
   at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
   at #=z59rPZHU9npaMpLWr7RO3U2i5RyaAblkFVfiwX_BqyPFt9VwX8w==.GetFolder(GetFolderType GetFolder1)
   at Aspose.Email.Clients.Exchange.WebService.EWSClient.GetEWSClient(String mailboxUri, ICredentials credentials, WebProxy proxy)
   at Aspose.Email.Clients.Exchange.WebService.EWSClient.GetEWSClient(String mailboxUri, ICredentials credentials)
   at OAuth_Test.Program.Main() in C:\Users\jdaughtry\source\repos\OAuth_Test\OAuth_Test\Program.cs:line 43

Here’s my code with presently confidential information redacted:

using Aspose.Email.Clients;
using Aspose.Email.Clients.Exchange;
using Aspose.Email.Clients.Exchange.WebService;
using Microsoft.Identity.Client;
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

namespace OAuth_Test
    class Program
        public static async Task<OAuthNetworkCredential> GetAzureToken(string clientId, string tenantId, string redirectUri, string user, string pass)
            var scopes = new string[] { "Mail.ReadWrite.All" };
            var app = PublicClientApplicationBuilder
                .WithAuthority(AzureCloudInstance.AzurePublic, tenantId)
            var result = await app.AcquireTokenByUsernamePassword(scopes, user, new NetworkCredential("", pass).SecurePassword).ExecuteAsync(CancellationToken.None);

            return new OAuthNetworkCredential(result.AccessToken);

        public static void Main()
                new Aspose.Email.License().SetLicense("Aspose.lic");

                var credentials = GetAzureToken(
                var builder = new ExchangeQueryBuilder();


                using (var client = EWSClient.GetEWSClient("", credentials))
                    var messageInfos = client.ListMessages("Inbox", builder.GetQuery(), false);

                    Console.WriteLine(string.Format("Found {0} unread messages", messageInfos.Count));
            catch (Exception ex)

Is there anything obvious that I’m missing?



I suggest you to please visit the following thread link for your kind reference.

Good day.

Yes, I’ve seen and followed everything in that thread. Unfortunately, the provided code needed some tweaking to compile and after tweaking it didn’t give me a valid OAuth token. Hence my current code that does result in a (presumably) valid token.

Perhaps instead of a snippet, you could provide me with a full sample that shows the usings and installed packages? One of my issues was a class I couldn’t find the reference assembly for even in NuGet.



Can you please explain the issue on your end. Perhaps, it would be good if you may please share the sample project that you are using on your end

I’ll send you the full project through a direct message because it contains some pieces of information I do not want publicly available such as email account credentials.


You can share the information privately with us. For this, you can click on my name icon in my post and then a popup will appear. Select the Message option to send the license file privately with me. Please do notify us here in this thread once you have shared the information privately.


Can you please confirm the URL on your end. I have tried to directly access that and getting 404 Not found error.

That’s the URL I was given in the Azure registration setup, but I also get a 404 when going to it directly. I also tried using{0}/oauth2/v2.0/token with {0} being replaced by the tenant, though in that case I get an error saying the token can’t be null or empty.


I have created an issue with ID EMAILNET-39925 as investigation to further investigate and resolve the issue. This thread has been linked with the issue so that you may be notified once the issue will be fixed.