jstedfast / MailKit

A cross-platform .NET library for IMAP, POP3, and SMTP.
http://www.mimekit.net
MIT License
6.17k stars 818 forks source link

`.Inbox` returning null #1701

Closed lucasnad27 closed 7 months ago

lucasnad27 commented 7 months ago

Describe the bug I'm using the SaslMechanismOAuth2 to connect to an instance of ImapClient. This code works for a majority of the email inboxes I'm connected to, but a couple of days ago, ~8 email clients started returning null when accessing the Inbox. More details below...

Platform

Exception If you got an exception, please include the exception Message and StackTrace. Nothing really helpful -- the stack trace I have is a null pointer exception encountered when calling GetSubfolders on the Inbox because it's returning null (see code example below)

To Reproduce Steps to reproduce the behavior: Because of the nature of this bug, I don't have any great reproduction steps, but I've provided some sample code below..

Expected behavior It should connect to the the user's Inbox and be able to access all subfolders when calling the GetSubfolders method.

Code Snippets

var scopes = new string[] {
    "email",
      "https://outlook.office.com/SMTP.Send",
    "https://outlook.office.com/IMAP.AccessAsUser.All",
};

using (var emailClient = new ImapClient(new ProtocolLogger("C:\\Temp\\imap.log")))
{
  authToken = publicClientApplication.AcquireTokenByUsernamePassword(scopes, username, password.ToSecureString()).ExecuteAsync();
  authToken.Wait();
  var oauth2 = new SaslMechanismOAuth2(authToken.Result.Account.Username, authToken.Result.AccessToken);
  emailClient.Connect(emailServer, emailPort, true);
  emailClient.Authenticate(oauth2);
  var inbox = emailClient.Inbox;
  // inbox is null
}

Protocol Logs Please include a protocol log (scrubbed of any authentication data), especially if you got an exception such as Syntax error in XYZ. Unexpected token: ....

NOTE: Working on getting logs prepped to ship

To get a protocol log, follow one of the following code snippets:

// log to a file called 'imap.log'
var client = new ImapClient (new ProtocolLogger ("imap.log"));

Note: if the protocol log contains sensitive information, feel free to email it to me at jestedfa@microsoft.com instead of including it in the GitHub issue.

Additional context Add any other context about the problem here.

A few things that have helped me while debugging:

jstedfast commented 7 months ago

This would only make sense, I think, if an INBOX couldn't be found. Do you have a protocol log that I could take a look at?

lucasnad27 commented 7 months ago

@jstedfast not yet on the protocol log. going to work on putting that together, but need to spent a bit of time getting the app running locally, as it usually runs as an Azure function.

fwiw - I can access these users' inbox (they are technically a "shared" inbox in MS Outlook) from my own outlook account, which has access -- my current working theory is that Inbox is null because of a silent permissions issue 🤷🏻‍♂️ -- hopefully that protocol log will give some additional hints.

lucasnad27 commented 7 months ago

One other piece of information, I tried Getting the first folder in the personal namespace, as shown below. That throws an index error, indicating that PersonalNamespaces is an empty list.

emailClient.GetFolder(emailClient.PersonalNamespaces[0]);

Working on that protocol log today.

jstedfast commented 7 months ago

That suggests to me that probably a NAMESPACE command failed. That's probably also why Inbox is null.

lucasnad27 commented 7 months ago

Hey @jstedfast I found a workaround for this authentication issue. Not pretty but it's going to do the job for now. I'm still actively engaged with MS support to track down the root cause. I will follow up on this ticket when I have more info -- in case it's helpful for error handling in Mailkit. Feel free to keep this issue open or closed in the meantime. Thanks for your quick responses on this 🙏🏻

jstedfast commented 7 months ago

Is this one of those Exchange authentication issues where it says "User authenticated but not connected"?

lucasnad27 commented 7 months ago

I haven't seen that anywhere in my logs. I do know that the imapClient.IsConnected property returns true.

jstedfast commented 7 months ago

It reports true in the case I mentioned as well because there is a connection.

The problem I mentioned is Exchange thinking that there is no connection. It's a very bizarre error that I think (if my memory is correct), it has something to do with the user's Office365 settings not allowing IMAP connections, but the correct OAuth2 credentials were supplied in the AUTH command.

This results in Exchange getting confused and returning all sorts of errors to various commands like NAMESPACE.

jstedfast commented 7 months ago

Here's an example: https://github.com/jstedfast/MailKit/issues/1494

StackOverflow has a number of these as well, but no logs that I saw in the first few clicks

https://stackoverflow.com/search?q=%5Bimap%5D%5Boffice365%5D+authenticated+but+not+connected

jstedfast commented 7 months ago

Since you are in direct communication with the MS Support, I'm going to close this because I'm 99% sure this is an Exchange issue (or a Office365 permissions issue).

I'll still be listening for notifications on this thread if there is anything you find out and if it is a MailKit issue, I'll reopen it (or you can reopen it).

The MS Support people are probably in a better position to help you at this point.