Hello,
We are using Aspose .NET Email to connect to a Microsoft Office 365 mail box using IMAP and a user id/password combination (Basic Authentication scheme) at the moment. But support for this approach of using a clear text password to connect to a Microsoft Office 365 mail box is going away by October 2020 and Microsoft recommends switching over to using OAuth2 authentication to access the mail box instead.
And, looking at the documentation for the latest version (20.x) of Aspose .NET Email, it looks like creating an IMAP client using OAuth2 is supported. We also managed to verify that we can indeed connect to a GSuite mail box using latest Aspose .NET Email using IMAP/OAuth2.
However, we have not been able to connect to an Office 365 mail box by creating an IMAP client using OAuth2. We used the Aspose email code samples to create the IMAP client and the code snippet is as given below. This code fails with an “Authentication failed” error at the line “client.SelectFolder(“Inbox”);”.
static void AccessExchangeIMAPServer(string accessToken)
{
Console.WriteLine("Connecting to Exchange over Imap...");
ImapMessageInfoCollection messageInfoCol;
try
{
using (ImapClient client = new ImapClient("outlook.office365.com", 993, "testemail@testdomain.com", accessToken, true))
{
client.SecurityOptions = SecurityOptions.Auto;
client.SelectFolder("Inbox");
messageInfoCol = client.ListMessages();
}
if (messageInfoCol != null && messageInfoCol.Count > 0)
{
foreach (var message in messageInfoCol)
{
Console.WriteLine(message.Subject.ToString());
}
}
}
catch(Exception e)
{
Console.WriteLine(e.Message.ToString() + "\r\n" + e.StackTrace);
}
}
Could you please tell us if Aspose .NET Email supports connecting to Office 365 mailbox using IMAP and OAuth2? If yes, could you please provide code snippets on how to achieve this?
The full error message with the stack trace is
Connecting to Exchange over Imap…
Authentication failed.
at #=ziT9KCqL0wjS2K5GK7UZEz_Atghgp.#=zSMnmBko=(IAsyncResult #=zVppe2xI=)
at #=ziT9KCqL0wjS2K5GK7UZEz_Atghgp.#=zMf8af9s=()
at #=zXL5SBveNLEHZztCytigYz9iW6Sr2$urwPBagw3A=.#=zcSL7t0Q=(#=zYT2p9TMhBQ7WSkaw8q5zjwsPVxKpUioC6Q== #=z7lfY7BI=)
at #=zfIjR1pi_K03Xao6g7KdrrzHqqWw3.#=z08aO9tWAztlJ(Int32 #=zGg5AMk4=, #=zYT2p9TMhBQ7WSkaw8q5zjwsPVxKpUioC6Q== #=z11kv_BM=)
at #=ziT9KCqL0wjS2K5GK7UZEz_Atghgp.#=zxaqcaIDbg_dh()
at #=z43Zg4xUstGvRjPVjujZpRuRaJdHwoqSegFIEVQ$L3auO.#=zxaqcaIDbg_dh()
at #=z9l1ogERylXofMpgT7EPk4krF0bu3eK_em517v0UQ8NNK…ctor(EmailClient #=zDvxBa8U=, String #=zSAFCSM8=, Nullable`1 #=zSYzqlMZuUGrt)
at Aspose.Email.Clients.Imap.ImapClient.BeginSelectFolder(IConnection connection, String folderName, Nullable`1 readOnly, AsyncCallback callback, Object state)
Please let me know if you need any more information.
Thank you,
Murali