The code examples supplied with MailKit suggest that accessing an IMAP mailbox requires code similar to this...
var at = "<<Access_Token>>";
using (var client = new ImapClient())
{
client.Connect("imap-mail.outlook.com", 993, SecureSocketOptions.SslOnConnect);
client.Authenticate("<<Email_Address>>", at);
// do stuff...
client.Disconnect(true);
}
Now when using this code with GMail and a properly generated and unexpired Access Token, this works a treat.
The same code, with, again, a properly generated Access Token just results in an AuthenticationException: Invalid username or password.
being thrown on the client.Authenticate
line.
Now I've tested against this website and I know that the email address and access token values are valid. What am I doing wrong here that is right in Gmail?
[EDIT] I found, on this MSDN page, the suggestion that...
Your app/server passes the access token to our IMAP service in the AUTHENTICATE command. We accept a base64-encoded string that contains:
- The user name.
- The authentication type Bearer for direct OAuth 2.0 requests.
- The access token granted by MSA.
For example, your app/server would base-64 encode this string:
user={user@domain.com}^Aauth=Bearer {Access Token}^A^A
where {user@domain.com} is the user's account, {Access Token} is the access token granted by MSA, and ^A are Ctrl-A characters (U+0001).
However, I've tried using creating the string suggested and passing that as the 'password' value in client.Authenticate()
to no avail.