jstedfast / MailKit

A cross-platform .NET library for IMAP, POP3, and SMTP.

Home Page:http://www.mimekit.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`.Inbox` returning null

lucasnad27 opened this issue · comments

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

  • OS: Azure Function runtime
  • .NET Runtime: .net core
  • .NET Framework: 4.8
  • MailKit Version: 2.0.6

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:

  • This only started failing a few days ago. No code changes or configuration changes have been made
  • I'm assuming this is an auth issue. I've inspected the emailClient's IsAuthenticated and IsConnected properties and both return true, but still returns a null Inbox

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?

@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.

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.

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

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 🙏🏻

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

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

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.

Here's an example: #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

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.