1 Reply Latest reply on Nov 16, 2018 8:47 AM by Mario Zahren

    Try to open MS Outlook OST file in C#

    Claire Divas

      I’m asking here this question because I’m getting no responses on other several C# forums.

      I have a C# application in which I’m trying to open OST file. After initialization of all functions, file path, file name and root folder. During testing of C# application error occur. First time I have seen weird error. To resolve this weird error, I have changed the source code and restarted the application again. While starting the application, same error again occur.

       

      The code which I have used in C# is:

       

      Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();

       

      NameSpace outlookNs = app.GetNamespace("MAPI");

      // Add PST file (Outlook Data File) to Default Profile

       

      • outlookNs.AddStore(pstFilePath);

       

      MAPIFolder rootFolder = outlookNs.Stores[pstName].GetRootFolder();

       

      Folders subFolders = rootFolder.Folders;

       

       

      I have tried to search problem regarding OST file in C# over this forum but can’t find any question.

      https://forum.solidworks.com/discussion/

       

      After using Google, I have found few approaches. In these approaches lots of coding involved. Probably this may solve problem.

      https://community.hortonworks.com/questions/226407/view-ost-file-in-c-language.html

      https://powershell.org/forums/topic/unable-to-open-ost-file-in-c-net/

      https://www.stellarinfo.com/convert-ost-to-pst.php

      https://community.rackspace.com/general/f/general-discussion-forum/8881/how-to-open-ost-file-using-c-code

      http://forums.codeguru.com/showthread.php?562219-How-Do-I-Open-OST-file-using-C

       

      Please suggest, which approach is better than other approaches.

        • Re: Try to open MS Outlook OST file in C#
          Mario Zahren

          What do you want to achieve with it?
          Do you want to write an email with outlook from your own account?

           

          using Outlook = Microsoft.Office.Interop.Outlook;
          
          bool notRunning = false;
          Outlook.MAPIFolder targetFolder = null;
          Outlook.Items folderItems = null;
          Outlook.MailItem mail = null;
          Outlook.MailItem oMsg = null;
          Outlook.NameSpace nameSpace = null;
          
          try
          {
              Outlook.Application oApp = null;
          
              // check if Outlook is running.
              if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
              {
                  oApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
              }
              else
              {
                  // if not, create a new Instance of Outlook and login with Default-Profile.
                  oApp = new Outlook.Application();
          
                  Process.Start("outlook.exe");
          
                  nameSpace = oApp.GetNamespace("MAPI");
          
                  targetFolder = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                  folderItems = targetFolder.Items;
                  mail = folderItems.Add(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
          
                  nameSpace.Logon("Outlook", "", Missing.Value, Missing.Value);
                  nameSpace = null;
                  notRunning = true;
              }
          
              // string mailSender = oApp.Session.CurrentUser.Address;
              // string senderProfile = oApp.Session.CurrentProfileName;
          
              if (notRunning == true)
              {
                  oMsg = mail;
              }
              else
              {
                  oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
              }
              
              oMsg.Send();
          }
          catch (COMException exp)
          {
              vault.GetErrorString(exp.ErrorCode, out string errorName, out string errorDesc);
              vault.MsgBox(0, errorDesc, EdmMBoxType.EdmMbt_OKOnly, errorName);
              this.SendMailOnError(m_vault, uebergabeWerteString1, errorName, errorDesc);
          }