Accessing Outlook via C# - COMException when checking a message's sender but not the subject?

0

I'm using Visual Studio and Outlook 2010. I am able to connect to my local Outlook default profile and list the messages in the Inbox, but I can only get the SentOn, Subject, and EntryID properties. As soon as I try to access the SenderName or Body (and a few others), I get the following exception:

System.Runtime.InteropServices.COMException was unhandled
  HResult=-2147467260
  Message=Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))
  Source=""
  ErrorCode=-2147467260
  StackTrace:
       at Microsoft.Office.Interop.Outlook._MailItem.get_SenderName()
       at reademail.Program.ReadMail() in h:\my documents\visual studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 54
       at reademail.Program.Main(String[] args) in h:\my documents\visual studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 20
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

The COMException is so vague it's maddening. I've searched and searched and can't find anything that has helped. I've tried delaying the system thread, and I've tried the MailItem.GetInspector trick for each item, too.

I am working in a corporate environment where I don't have local admin privileges on my machine, and neither will the individual who uses the software. Getting local admin will be a pain in rear, so I'd like to be really sure that's the problem before starting the process.

Here's the minimal code that produces the error. I would greatly appreciate any help!

using System;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace MinimalExample
{
    class Program
    {
        static void Main(string[] args)
        {
            //Correctly triggers my local Outlook to open, allowing me to select the desired profile
            Outlook.Application app = new Outlook.Application();
            Outlook._NameSpace ns = app.GetNamespace("MAPI");

            //Correctly opens the Inbox and reports the correct stats
            Outlook.MAPIFolder inboxFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            Console.WriteLine("Folder Name: {0}", inboxFolder.Name);
            Console.WriteLine("Num Items: {0}", inboxFolder.Items.Count.ToString());

            for (int counter = 1; counter <= inboxFolder.Items.Count; counter++)
            {
                //This cast ensures you only deal with valid MailItems and not calendar issues
                Outlook.MailItem item = inboxFolder.Items[counter] as Outlook.MailItem;
                if (item != null)
                {
                    //The following works fine
                    Console.WriteLine("Item: {0}", counter.ToString());
                    Console.WriteLine("EntryID: {0}", item.EntryID);
                    Console.WriteLine("Subject: {0}", item.Subject);
                    Console.WriteLine("Sent: {0} {1}", item.SentOn.ToLongDateString(), item.SentOn.ToLongTimeString());

                    //Enabling any of the following two lines causes the E_ABORT COMException
                    //Console.WriteLine("Sendername: {0}", item.SenderName);
                    Console.WriteLine("Body: {0}", item.Body);
                }
            }
            Console.ReadLine();
        }
    }
}

EDIT: What I ended up doing

Doing the software as a plugin sidestepped the security issues, but this is far from ideal. I just can't believe that .NET can't do this sort of work without local admin access. If anybody knows of a way to accomplish this as a standalone console app without local admin, I would love to hear. Many thanks!

c#
outlook
office-interop
comexception
asked on Stack Overflow Feb 2, 2016 by Aaron • edited Feb 3, 2016 by Aaron

2 Answers

1

https://social.msdn.microsoft.com/Forums/vstudio/en-US/f0a76f44-704e-4454-b44e-c95b3f60eff5/cant-send-email?forum=vsto

Here is another type of problem with same exception.

There is an answer like that: "...In other words, if you want full access to the Outlook object model, so that methods like Send and SaveAs work unimpeded, that's the Outlook.Applicatiom object you need to use."

And here is another one with same error code:

urlDownloadToFile error 2147467260 in VB6

I think it's all about your account privileges.

answered on Stack Overflow Feb 2, 2016 by Fırat Çağlar Akbulut • edited May 23, 2017 by Community
0

Do you mean you run into the security prompt? Where is your code executed?

You need to either have an up-to-date anti-virus app installed or use Extended MAPI (C++ or Delphi) or Redemption (it wraps Extended MAPI) to bypass the security prompts. See http://www.outlookcode.com/article.aspx?id=52 for more.


User contributions licensed under CC BY-SA 3.0