7 Replies Latest reply on Jan 12, 2017 2:50 AM by Mario Zahren

    GarbageCollector does not work in EPDM AddIn

    Mario Zahren

      I try to send an email in my addin.

      For this I start Outlook if it has not yet started with:

       

      bool notRunning = false;
      Outlook.MAPIFolder targetFolder = null;
      Outlook.Items folderItems = null;
      Outlook.MailItem mail = null;
      Outlook.MailItem oMsg = null;
      
      Outlook.Application oApp = null;
      
      // Prüfen ob Outlook-Prozess läuft.
      if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
      {
          // Wenn läuft, dann mit GetActiveObject den Prozess aufgreifen und zur Application casten.
          oApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
      }
      else
      {
          // Wenn  nicht läuft, dann neue Instanz von Outlook erstellen und mit Default-Profil einloggen.
          oApp = new Outlook.Application();
      
          Outlook.NameSpace 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;
      string excelFileMail = AenderungsNummerVal + "_" + ArtNumberValKomp + "_" + RevNumberFn;
      
      if (notRunning == true)
      {
          oMsg = mail;
      }
      else
      {
          oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
      }
      
      oMsg.Send();
      releaseObject(oRecips);
      releaseObject(oMsg);
      releaseObject(oApp);
      
      private void releaseObject(object obj)
      {
          try
          {
              System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
              obj = null;
          }
          catch (Exception ex)
          {
              obj = null;
              MessageBox.Show("Unable to release the Object " + ex.ToString());
          }
          finally
          {
              GC.Collect();
              GC.WaitForPendingFinalizers();
      
              GC.Collect();
              GC.WaitForPendingFinalizers();
          }
      }
      

       

      But unfortunately the process Outlook.exe is not unloaded so that the next mail at this point produces an error:

       

      // Prüfen ob Outlook-Prozess läuft.
      if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
      {
          // Wenn läuft, dann mit GetActiveObject den Prozess aufgreifen und zur Application casten.
          oApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
      }
      

       

      "Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))"

       

      How can I remove process Outlook.exe?

       

      Any help would be appreciated.

      Thanks, Mario