I'm using MaikKit, and am trying to work out how to get the mail in a Jim
folder I set up in a Gmail account. I tried enumerating the folders as follows...
private void GetFolders() {
using ImapClient client = new();
EmailAccount emailAccount = EmailAccountOptions.Value;
client.Connect(emailAccount.Server, emailAccount.Port, SecureSocketOptions.SslOnConnect);
client.Authenticate(emailAccount.UserName, emailAccount.Password);
foreach (FolderNamespace ns in client.PersonalNamespaces) {
IMailFolder folder = client.GetFolder(ns);
_msg += $"<br/>Ns: {ns.Path} / {folder.FullName}";
foreach (IMailFolder subfolder in folder.GetSubfolders()) {
_msg += $"<br/> {subfolder.FullName}";
}
}
try {
IMailFolder jim = client.GetFolder(new FolderNamespace('/', "Jim"));
jim.Open(FolderAccess.ReadOnly);
_msg += $"<br/>Got Jim, has {jim.Count} email(s)";
}
catch (Exception ex) {
_msg += $"<br/>Ex ({ex.GetType()}): {ex.Message}";
}
}
This shows the following...
Ns: /
INBOX
Jim
[Gmail]
✔
✔✔
Got Jim, has 1 email(s)
So, it seems I can access Jim
without problem. As the purpose of this was to access Jim
only, the enumeration isn't needed. However, when I removed it, I got an exception...
Ns: /
Ex (MailKit.FolderNotFoundException): The requested folder could not be found
After some trial and error, I found out that the following works...
private void GetFolders() {
using ImapClient client = new();
EmailAccount emailAccount = EmailAccountOptions.Value;
client.Connect(emailAccount.Server, emailAccount.Port, SecureSocketOptions.SslOnConnect);
client.Authenticate(emailAccount.UserName, emailAccount.Password);
foreach (FolderNamespace ns in client.PersonalNamespaces) {
IMailFolder folder = client.GetFolder(ns);
var subs = folder.GetSubfolders();
}
try {
IMailFolder jim = client.GetFolder(new FolderNamespace('/', "Jim"));
jim.Open(FolderAccess.ReadOnly);
_msg += $"<br/>Got Jim, has {jim.Count} email(s)";
}
catch (Exception ex) {
_msg += $"<br/>Ex ({ex.GetType()}): {ex.Message}";
}
}
...but if I remove the call to folder.GetSubfolders()
it throws an exception.
If I change the code to look for INBOX
instead...
IMailFolder jim = client.GetFolder(new FolderNamespace('/', "INBOX"));
...then it works fine even without the call to folder.GetSubfolders()
.
Anyone any idea why this happens? As far as I can see, the foreach
loop isn't doing anything that should affect Jim
.