Can not retrieve EWSClient instance

I’m trying to retrieve an instance of my outlook calendar. When I run this code:

        const string domain = @"https://outlook.office365.com/owa/<my company name>.com/";
        const string username = @"<my email username>";
        const string password = @"<my email password>";
        IEWSClient client = EWSClient.GetEWSClient(domain, username, password);

I get a 404 error when executing the GetEWSClient.

I also tried this:

        const string mailboxUri = "https://outlook.office365.com/ews/exchange.asmx";
        const string domain = @"https://outlook.office365.com/owa/<my company name>/";
        const string username = @"<my email username>";
        const string password = @"<my email password>";
        NetworkCredential credentials = new NetworkCredential(username, password, domain);
        IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credentials);

but I get a 404 error on this as well.

Any ideas what I dont have set up properly?

Thank you.

Hello @jonathn6,

It looks like you are specifying the wrong domain, based on the 404 error. Try the following:

IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "your.username", "your.password");

Also, please refer to the following articles in our documentation:

Thank you.

Thank you for your response. Of course, when I read your response and saw you reference a 404 error, I hit myself on the head. I’m getting a 401 error, not a 404 error. So I AM reaching the server, I just dont have the proper sign in information being sent. I’m not sure why that is because I am using the same email address and password that I use to log into my microsoft account on-line. This is the code that I am now trying to execute:

        const string mailboxUri = "https://outlook.office365.com/ews/exchange.asmx";
        const string domain = @"intersoftassociates.com";
        const string username = @"Jonathan.Small";
        const string password = @"<my password>";
        NetworkCredential credentials = new NetworkCredential(username, password, domain);
        IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credentials);
        return client;

@jonathn6,

Thank you for the additional information.
Most likely your problem is that Microsoft has recently discontinued support for Basic Authentication on client apps. Try using Modern Authentication. We have the doc and the same blog article on how to do this.
Also, I have attached a test app using Modern Authentication: EWSModernAuthenticationApp.zip (3.4 KB)
All you have to do is specify your credentials there.

Thank you.

@DmitryS,
Thank you for this information! I read through the blog and had to log into the Azure portal to register my app (which I did). I got the app id, tenantID, and client secret. I assume the username will be “jonathan.small” since my email address is jonathan.small@intersoftassociates.com.

I populated the fields in the appsettings.json file as instructed and ran the application with a break point on this line:

if (accessToken != null)

I hit the breakpoint and added accessToken to the watch and I can see an accessToken value so I know I connected to microsoft and retrieved the token. Then I clicked on the continue button and I get this screen:

figure 1a.jpg (294.3 KB)

I ran the application a second time and once I hit the breakpoint, I stepped through the code. The error screen that I provided is generated as a result of trying to execute this line of code:

 using var client = EWSClient.GetEWSClient("https://outlook.office365.com/EWS/Exchange.asmx", credentials);

Any suggestions?

@jonathn6,

I think the issue is related with Visual Studio settings. Please check the following settings: 1.png (24.7 KB)

Thank you.

Thanks. I made this change in my VS settings. I also found a page on the Azure active directory admin page which shows my project owners. It tells me that my user name is something other than what I thought. So I changed the value in the appsettings.json file to the value that Azure says and I reran the code. Now I get a “The request failed with HTTP status 403: Forbidden” message when trying to execute

using var client = EWSClient.GetEWSClient("https://outlook.office365.com/EWS/Exchange.asmx", credentials);

Now I’m a bit confused. I was able to retrieve an access token, so that implies to me that I have provided the correct information in the appsettings.json file. Otherwise, I would not get back an access token. So who would be forbidding me access when trying to connect to the EWS/Exchange.asmx?

Maybe I’m going about this the wrong way. My goal is to use Aspose to sync my outlook calendar to my google calendar. Will the Aspose.Email product provide me with the necessary tools to accomplish this?

@jonathn6,

Now I get a “The request failed with HTTP status 403: Forbidden” message when trying to execute

Using Modern Auth requires attention when setting up the app on the Azure active directory page.
We provide only client-side functionality and cannot control the server-side settings.
If you are using a confidential client, make sure that the permission is of the Application type and not delegated. Set the full_access_as_app. This will give access to the mailbox and will require administrator approval first. Are you an administrator? All application-level permissions require administrator approval 1.png (28.2 KB).

My goal is to use Aspose to sync my outlook calendar to my google calendar. Will the Aspose.Email product provide me with the necessary tools to accomplish this?

Perhaps the following information will be helpful to you.

Thanks.