I would like to put the mail folder structure of an imap account by gmail into a treeview wpf structure and get FolderNotOpenException
.
Here is the piece of code how I am attempting to achieve this goal:
private void tree_Loaded(object sender, RoutedEventArgs e)
{
foreach (AccountData adata in aDatas)
{
// ... Create a TreeViewItem.
TreeViewItem item = new TreeViewItem();
item.Header = adata.credential.UserName;
cancel = new System.Threading.CancellationTokenSource();
System.Threading.CancellationToken token = cancel.Token;
client = GetIMAPClient(adata.uri,adata.credential,cancel);
var folders = GetFolders(client, token);
item.ItemsSource = folders;
foreach(IMailFolder folder in folders)
{
IEnumerable<IMailFolder> subFolders = folder.GetSubfolders(false,cancel.Token);
if (subFolders.Count() > 1)
{
foreach (IMailFolder subFolder in subFolders)
{
subFolder.Open(FolderAccess.ReadOnly);
var subItem = new TreeViewItem();
//if (subFolder.IsOpen)
{
subItem.ItemsSource = subFolders;
item.Items.Add(subItem);
}
}
}
}
// ... Get TreeView reference and add both items.
var tree = sender as TreeView;
tree.Items.Add(item);
}
}
Can someone please help to overcome this problem and to access subfolders ? Thanks