For future readers: If you encounter any issue with authentication to Microsoft 365 (formerly Office 365), upgrade to a an up-to-date version of Rebex Secure Mail and try our Office 365 / OAuth 2.0 sample apps.
It looks like you are calling Imap.Login(string token, ImapAuthentication method)
method, but your token
is incorrectly formatted. Instead of binary \x1 characters (characters with ASCII code of 1), the token uses "\x1" strings. In practice, the following C# code would reproduce the issue:
// prepare (wrap) the authentication token for IMAP, POP3, or SMTP
string pattern = string.Format("user={0}{1}auth=Bearer {2}{1}{1}",
_account.Username,
"\\x1", // <-- this is wrong!
_accessToken);
string token = Convert.ToBase64String(Encoding.ASCII.GetBytes(pattern));
// authenticate using the (wrongly) wrapped access token
client.Login(token, ImapAuthentication.OAuth20);
To fix the issue, use the proper \x1 character:
// prepare (wrap) the authentication token for IMAP, POP3, or SMTP
string pattern = string.Format("user={0}{1}auth=Bearer {2}{1}{1}",
_account.Username,
"\x1", // <-- this is correct
_accessToken);
string token = Convert.ToBase64String(Encoding.ASCII.GetBytes(pattern));
// authenticate using the wrapped access token
client.Login(token, ImapAuthentication.OAuth20);
Alternatively, upgrade to Rebex Secure Mail R5.7 or later, which eliminates the need to manually encode the token, making it possible to replace the code above with a single line of code:
// authenticate using the token (and proper wrapping)
client.Login(_account.Username, _accessToken, ImapAuthentication.OAuth20);