I can't connect to Microsoft Office 365 mailbox using Aspose.Email

Hello team,

How can I fetch the emails from Microsoft Office365 account, please?
It seems that we can’t use the basic authentication with Microsoft Office365.

This is how I’m trying to access it

ImapClient client = new ImapClient(host, port,userName, password);

foreach (ImapFolderInfo folder in client.ListFolders(true))
{
lstFolders.Items.Add(new ListViewItem(new String[] { folder.Name, folder.TotalMessageCount.ToString() }));
}

aspose-email-error.PNG (51.0 KB)

I also tried the following but I’m getting timeout exception
ImapClient client = new ImapClient(host, port,userName, password, true);

Hello @ibrahimahmed1985,

From some time, you cannot use basic authentication to connect to Microsoft 365.
Please refer to the article on using modern authentication.

Also, I’m forwarding you an example application, at your request from another post.
ModernAuthenticationImap.zip (3.9 KB)

Thanks @margarita.samodurova
I will try that

@ibrahimahmed1985 ,

Please spare a minute to share your feedback. And feel free to ask any other further questions.

Hello @margarita.samodurova
Sorry for the delay.

I followed the instructions in the above document but I faced an issue.
The access token is returned successfully but when trying to initialize ‘EWSClient’ it throws the following error
“Cannot access a disposed object.\r\nObject name: ‘System.Net.HttpWebResponse’.”

This is the line that throws that error

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

office365.png (227.3 KB)

Please advice

Hello @ibrahimahmed1985 ,

Try to run the code without try-catch statement. After that let us know what exception you face.

Hello @margarita.samodurova

I’m getting the same error also without the try and catch
image.png (108.0 KB)

@ibrahimahmed1985 ,

Please try to use your credentials as in the following example
EWSModernAuthenticationApp.zip (3.7 KB). It must work. Spare a minute to share your feedback.

Hello @margarita.samodurova

Unfortunately still getting an error. But it is a different error now.
I’m getting a 403 status code although the access token is returned successfully.

image.png (146.4 KB)

Thanks

Never mind @margarita.samodurova

It works fine now.
I had to add ‘full_access_as_app’ permission to the APP

Thanks for your help

You’re welcome @ibrahimahmed1985!

If you have further questions, feel free to ask.

Hello, @margarita.samodurova ,

How can I connect using POP3 client not EWSClient, please?

Thanks

Hello @ibrahimahmed1985,

Please explore the article section: Use Modern Authentication with IMAP, POP or SMTP Clients

The sample code will be similar to SMTP and IMAP except for Outlook resource URLs, for POP3 it will be as follows: https://outlook.office.com/POP.AccessAsUser.All.

Also, please check the related example:
EWSModernAuthenticationPop.zip (3.6 KB)

Thanks @margarita.samodurova but I want to connect to Office 365 using the Client Credential flow (App Auth, No user interaction).

I tried the sample in the shared document for app-auth but it doesn’t work

Oh, I can see this sentence in the shared document

IMAP, POP, SMTP access via application permissions isn’t supported

Is this true, please?

@ibrahimahmed1985,

Yes, it is, if two-factor authentication is enabled, an interactive window will appear when connecting to MS 365 via IMAP, POP3, SMTP to enter username/password.
This is a feature of Microsoft identity platform and we can’t affect it.