Free Support Forum -

How do you use Apose.Email with OAuth?

We are thinking about using Aspose.Email (EWS) v19.8.0 for a .NET solution that should read emails from an Exchange server.

Given Microsoft’s announcement on upcoming changes to Exchange Web Services (EWS) API for Office 365:

It would seem wiser to consider using OAuth instead of Basic Authentication? But how exactily do you do that with Aspose.Email?

I have been using this guide to connect to Microsoft’s API using OAuth.
“Microsoft : Authenticate an EWS application by using OAuth (and how to register application in Microsoft Azure.)”

It takes these parameters to work:
appId : Guid from Microsoft Azure after registering my App
tenantId : Guid from Microsoft Azure after registering my App
ewsScopes : “
mailboxUri : “

The only documentation I have been able to find on Aspose.Email and OAuth is here:

Aspose.Email – OAuth Support for EWS with Office 365

Your code example looks like this:

string authority = ConfigurationManager.AppSettings[“authority”];
string clientID = ConfigurationManager.AppSettings[“clientID”];
Uri clientAppUri = new Uri(ConfigurationManager.AppSettings[“clientAppUri”]);
string resource = ConfigurationManager.AppSettings[“resource”];
AuthenticationContext authenticationContext = new AuthenticationContext(authority, false);
AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resource, clientID, clientAppUri);
OAuthNetworkCredential oAuthCredential = new OAuthNetworkCredential(authenticationResult.AccessToken);
using (IEWSClient client = EWSClient.GetEWSClient(resource + “ews/exchange.asmx”, oAuthCredential))
ExchangeMessageInfoCollection messageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri);

What are the correct values for the parameters and where do I get/find them? (I assume some of them are equal to the ones used with Microsoft’s API?)

I have not been able to get it to work. Can you give me a .NET code example with some real example values?


I have observed your requirements and suggest you to please visit this documentation link to serve the purpose on your end. I hope the shared information will be helpful.

Your link points to exactly the same code example I quoted above in my initial question. It is a bit outdated and it doesn’t provide any example values.
I have tried to alter the code to make it work. It now looks like this:

string appId = “[guid from Azure]”;
string tenantId = “[guid from Azure]”;
string mailboxUri = “”;
Uri clientAppUri = new Uri(“”);
string resource = “”;
string authority = “” + tenantId;

var authenticationContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authority, false);

var platformParams = new PlatformParameters(PromptBehavior.Auto);
var authenticationResult = authenticationContext.AcquireTokenAsync(resource, appId, clientAppUri, platformParams);
OAuthNetworkCredential oAuthCredential = new OAuthNetworkCredential(authenticationResult.Result.AccessToken);

IEWSClient client = EWSClient.GetEWSClient(mailboxUri, oAuthCredential);

It doesn’ work though. I get a token back, but the call EWSClient.GetEWSClient returns an error:
System.Net.WebException: ‘The request failed with HTTP status 401: Unauthorized.’

Unless I also provide my credentials like this:
oAuthCredential.UserName = “[my userid]”;
oAuthCredential.Password = “[my password]”;

This can’t be right, can it? I don’t have to provide explicit user credentials to use Microsoft’s API with OAuth…


Than you for sharing the information with us. An issue with ID EMAILNET-39596 has been created in our issue tracking system 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.

The issues you have found earlier (filed as EMAILNET-39596) have been fixed in this update.

1 Like