'Create mail where Outlook is not running in Administrative rights

The below code works in my desktop application if:

  • Outlook is not running
  • Outlook is running in Administrator rights

When Outlook is running normally, the code throws an error.

Object reference is not found

Another problem is, I can not ask users to keep running Outlook in Administrator rights because the instant search stops working.

My application runs in Administrative rights which is compulsory for my application.

Platform: Microsoft.NET Framework 4.6.2 Outlook 2016 Windows 10

 public static void CreateOutlookEmail(string email, string subject, string body, string cc, string fileName)
    {
        try
        {
            Microsoft.Office.Interop.Outlook.Application app;
            try
            {
                app = (Microsoft.Office.Interop.Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
            }
            catch
            {
                app = new Microsoft.Office.Interop.Outlook.Application();
            }

            if (app == null)
            {
                return;
            }

            Microsoft.Office.Interop.Outlook.MailItem mailItem = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem) as
                                                                 Microsoft.Office.Interop.Outlook.MailItem;
            mailItem.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
            mailItem.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh;
            mailItem.Subject = subject;
            mailItem.To = email;
            mailItem.HTMLBody = body;
            mailItem.CC = cc;

            var fileInfo = new FileInfo(fileName);

            mailItem.Attachments.Add(fileName, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, 1,
                                     fileInfo.Name);
            //mailItem.Display();


            ((Microsoft.Office.Interop.Outlook._MailItem)mailItem).Send();

        }
        catch (Exception eX)
        {
            XtraMessageBox.Show(eX.Message + "\n" + eX.StackTrace + "\n" + eX.Source + "\n" + eX.InnerException);
        }
    }


Solution 1:[1]

COM system will not marshal calls between applications running in different security contexts (e.g. a regular app and one running with elevated rights as administrator). There is how it is designed to work.

One possible workaround is to avoid using Outlook Object Model (out-of-proc) and use Extended MAPI (which is loaded in-proc), but that limits you to C++ or Delphi. You can also use an Extended MAPI wrapped (such as CDO 1.21, which is no longer supported by Microsoft) or Redemption (I am its author - its RDOSession object is similar to Session in CDO 1.21 or Namespace object in OOM). Note that you might still have problem as PST and cached Exchange store provider would not be able to share PST and OST files used in the same profile but different MAPI sessions between processes running in different security contexts.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1