We were having a bit of a problem at a client when trying to open a mail folder and it couldn't be found so I created the below method to try my best to find the folder namespace that the mail folder was under.
private FolderNamespace FindFolderNamespace(ImapClient imapClient, string folderName)
{
string[] folderNameParts = folderName.Split('/');
FolderNamespaceCollection folderNamespaces = new FolderNamespaceCollection();
foreach (FolderNamespace folderNamespace in imapClient.PersonalNamespaces) folderNamespaces.Add(folderNamespace);
foreach (FolderNamespace folderNamespace in imapClient.SharedNamespaces) folderNamespaces.Add(folderNamespace);
foreach (FolderNamespace folderNamespace in imapClient.OtherNamespaces) folderNamespaces.Add(folderNamespace);
Dictionary<FolderNamespace, string> potentialFolderNamespaces = new Dictionary<FolderNamespace, string>();
foreach (FolderNamespace folderNamespace in folderNamespaces)
{
IMailFolder mailFolder = imapClient.GetFolder(folderNamespace);
foreach (string folderNamePart in folderNameParts)
{
if (mailFolder.GetSubfolders().Any(mf => mf.Name.Equals(folderNamePart, StringComparison.OrdinalIgnoreCase)))
{
mailFolder = mailFolder.GetSubfolder(folderNamePart);
}
else
{
break;
}
}
if (mailFolder.FullName.IndexOf(folderName, StringComparison.OrdinalIgnoreCase) >= 0)
{
return folderNamespace;
}
if (!mailFolder.IsNamespace)
{
potentialFolderNamespaces.Add(folderNamespace, mailFolder.FullName);
}
}
FolderNamespace closestFolderNameSpace = potentialFolderNamespaces.OrderByDescending(n => n.Value.Length).FirstOrDefault().Key;
if (closestFolderNameSpace != null)
{
return closestFolderNameSpace;
}
FolderNamespace defaultFolderNamespace = folderNamespaces.FirstOrDefault();
return defaultFolderNamespace;
}
However, this didn't work. On closer inspection it seems that PersonalNamespaces, SharedNamespaces and OtherNamespaces are all empty and so there are no folder namespaces for this account.
As a quick check I just tried the below:
imapClient.GetFolder(folderName)
However, this threw an ImapProtocolException saying:
The IMAP server has unexpectedly disconnected.
Trying with only the root folder name also did the same thing.
I'm at a bit of a loss how I can open a folder on this account as I can't seem to find it. How can I do it?
Some more details:
- .Net 4.6.1
- MailKit/MimeKit versions 1.1 but updating to 2.4.1 didn't help
- Office365 IMAP account
Protocol log:
Connected to --cut--
S: * OK The Microsoft Exchange IMAP4 service is ready. --cut--
C: A00000000 CAPABILITY
S: * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+
S: A00000000 OK CAPABILITY completed.
C: A00000001 AUTHENTICATE XOAUTH2 --cut--
S: A00000001 NO AUTHENTICATE failed.
C: A00000002 AUTHENTICATE PLAIN --cut--
S: A00000002 OK AUTHENTICATE completed.
C: A00000003 CAPABILITY
S: * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CLIENTACCESSRULES CLIENTNETWORKPRESENCELOCATION BACKENDAUTHENTICATE CHILDREN IDLE NAMESPACE LITERAL+
S: A00000003 OK CAPABILITY completed.
C: A00000004 NAMESPACE
S: A00000004 BAD User is authenticated but not connected.
C: A00000005 LIST "" "INBOX"
S: A00000005 BAD User is authenticated but not connected.
S: * BYE Connection closed. 14