Hi ,
I am developing a windows application using C#. In that I want to display all the outlook profiles in a listbox and on selecting one, The folder structure should be displayed on a treeview control.
I can display all the profiles in the listbox and on its changedindex event I am calling the function to load all outlook folders(and add that to the treeview).
When i run the application first time it is working properly , but when i select the other profile it loads the folder structure of firstprofile it self...
public void LoadOutlookFolders(string MSOprofile)
{
try
{
Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Outlook.NameSpace ns = app.GetNamespace("MAPI");
ns.Logon(MSOprofile, _missing, false, true);
TreeNode rootnode = treeFolders.Nodes.Add("MS Outlook");
rootnode.Tag = -1;
for (int i = 1; i <= ns.Folders.Count; i++)
{
Outlook.MAPIFolder cFolder = ns.Folders[i];
TreeNode FLevelNode = rootnode.Nodes.Add(cFolder.Name);
FolderParam flParam = new FolderParam();
flParam.Name = cFolder.Name;
flParam.EntryId = cFolder.EntryID;
flParam.StoreId = cFolder.StoreID;
flParam.Type = "Outlook";
flParam.DefaultMessageClass = cFolder.DefaultMessageClass;
FLevelNode.Tag = flParam;
for (int j = 1; j <= cFolder.Folders.Count; j++)
{
if (cFolder.Folders[j].DefaultMessageClass == "IPM.Note" || cFolder.Folders[j].DefaultMessageClass == "IPM.Contact")//Add only the messages and contact folders(excluding calendar,notes,....
{
TreeNode sLevelNode = FLevelNode.Nodes.Add(cFolder.Folders[j].Name);
displayOutlook(cFolder.Folders[j], sLevelNode);
FolderParam fldrParam = new FolderParam();
fldrParam.Name = cFolder.Folders[j].Name;
fldrParam.EntryId = cFolder.Folders[j].EntryID;
fldrParam.StoreId = cFolder.Folders[j].StoreID;
fldrParam.Type = "Outlook";
fldrParam.DefaultMessageClass = cFolder.Folders[j].DefaultMessageClass;
sLevelNode.Tag = fldrParam;
cFolder.Folders[j].Items.Session.Logoff();
cFolder.Folders[j].Application.Session.Logoff();
cFolder.Folders[j].Session.Logoff();
}
}
ns.Folders[i].Session.Logoff();
ns.Folders[i].Items.Session.Logoff();
ns.Folders[i].Application.Session.Logoff();
cFolder.Items.Session.Logoff();
cFolder.Application.Session.Logoff();
cFolder.Session.Logoff();
}
if (rootnode != null && rootnode.FirstNode != null)
{
rootnode.ExpandAll();
}
ns.DefaultStore.Session.Logoff();
ns.CurrentUser.Session.Logoff();
ns.Accounts.Session.Logoff();
ns.Session.Logoff();
ns.Logoff();
app.Session.Logoff();
app.Quit();
insertOutlookProfiles();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void displayOutlook(Outlook.MAPIFolder folder, TreeNode rootnode)
{
for (int j = 1; j <= folder.Folders.Count; j++)
{
TreeNode node = rootnode.Nodes.Add(folder.Folders[j].Name);
FolderParam flparam = new FolderParam();
flparam.Name = folder.Folders[j].Name;
flparam.EntryId = folder.Folders[j].EntryID;
flparam.StoreId = folder.Folders[j].StoreID;
flparam.Type = "Outlook";
flparam.DefaultMessageClass = folder.Folders[j].DefaultMessageClass;
node.Tag = flparam;
if (folder.Folders[j].Folders.Count > 0)
{
displayOutlook(folder.Folders[j], node);
folder.Folders[j].Items.Session.Logoff();
folder.Folders[j].Application.Session.Logoff();
folder.Folders[j].Session.Logoff();
}
else
{
folder.Folders[j].Items.Session.Logoff();
folder.Folders[j].Application.Session.Logoff();
folder.Folders[j].Session.Logoff();
}
}
folder.Items.Session.Logoff();
folder.Application.Session.Logoff();
folder.Session.Logoff();
}
Can any one help me in this Or Is there is any other way to achieve this?
Thanking you in advance,
Jaz