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

Hi Jaz,

i am developing a windows application using c#. In that I want to display all the outlook profiles. can u send me the code for that.

Thanks in advance..
yeah...

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

Hi I am also facing same isssue like you.do you have any idea how to resolve this one

Do you really think the original poster is waiting around after 5 years to give you the code?

 public void Dispose()
        {
            //throw new NotImplementedException();
            if (OutlookNS != null)
            {
                while (System.Runtime.InteropServices.Marshal.FinalReleaseComObject(this.OutlookNS) > 0) { }
            }
            if (Inbox != null)
            {
                while (System.Runtime.InteropServices.Marshal.FinalReleaseComObject(this.Inbox) > 0) { }
            }
            if (DeletedFolder != null)
            {
                while (System.Runtime.InteropServices.Marshal.FinalReleaseComObject(this.DeletedFolder) > 0) { }
            }
            if (BackupFolder != null)
            {
                while (System.Runtime.InteropServices.Marshal.FinalReleaseComObject(this.BackupFolder) > 0) { }
            }
            this.OutlookApp.Quit();
            while (System.Runtime.InteropServices.Marshal.FinalReleaseComObject(this.OutlookApp) > 0) { }
            this.OutlookApp = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

 public void Dispose()
        {
            //throw new NotImplementedException();
            if (OutlookNS != null)
            {
                while (System.Runtime.InteropServices.Marshal.FinalReleaseComObject(this.OutlookNS) > 0) { }
            }
            if (Inbox != null)
            {
                while (System.Runtime.InteropServices.Marshal.FinalReleaseComObject(this.Inbox) > 0) { }
            }
            if (DeletedFolder != null)
            {
                while (System.Runtime.InteropServices.Marshal.FinalReleaseComObject(this.DeletedFolder) > 0) { }
            }
            if (BackupFolder != null)
            {
                while (System.Runtime.InteropServices.Marshal.FinalReleaseComObject(this.BackupFolder) > 0) { }
            }
            this.OutlookApp.Quit();
            while (System.Runtime.InteropServices.Marshal.FinalReleaseComObject(this.OutlookApp) > 0) { }
            this.OutlookApp = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

Use above code to kill outlook objects etc

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.