通过C#访问Outlook - 检查邮件的发件人但不是主题时出现COMException?

时间:2016-02-02 15:30:24

标签: c# outlook office-interop comexception

我正在使用Visual Studio和Outlook 2010.我能够连接到本地Outlook默认配置文件并列出收件箱中的邮件,但我只能获取SentOn,Subject和EntryID属性。一旦我尝试访问SenderName或Body(以及其他一些),我就会遇到以下异常:

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: 

COMException太模糊了,令人发狂。我进行了搜索和搜索,无法找到任何有帮助的内容。我试过延迟系统线程,我也为每个项目尝试了MailItem.GetInspector技巧。

我在公司环境中工作,我的机器上没有本地管理员权限,使用该软件的个人也不会。获得本地管理员将是一个痛苦的后方,所以我想在开始这个过程之前确定问题是什么。

这是产生错误的最小代码。我非常感谢任何帮助!

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();
        }
    }
}

编辑:我最终做了什么

将软件作为插件来回避安全问题,但这远非理想。我无法相信.NET在没有本地管理员访问权限的情况下无法完成这类工作。如果有人知道如何通过一个独立的控制台应用程序而不是本地管理员来实现这一目标,我很乐意听到。非常感谢!

2 个答案:

答案 0 :(得分:1)

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

这是另一种同样例外的问题。

有这样的答案: &#34; ...换句话说,如果您想要完全访问Outlook对象模型,那么Send和SaveAs等方法可以畅通无阻地运行,那就是您需要使用的Outlook.Applicatiom对象。&#34 ;

这是另一个有相同错误代码的人:

urlDownloadToFile error 2147467260 in VB6

我认为这完全取决于您的帐户权限。

答案 1 :(得分:0)

你的意思是你遇到了安全提示吗?你的代码在哪里执行?

您需要安装最新的防病毒应用程序或使用扩展MAPI(C ++或Delphi)或Redemption(它包装扩展MAPI)以绕过安全提示。有关详情,请参阅http://www.outlookcode.com/article.aspx?id=52