繁体   English   中英

System.Runtime.InteropServices.COMException (0x80004004): 操作中止 (0x80004004 (E_ABORT))

[英]System.Runtime.InteropServices.COMException (0x80004004): Operation aborted (0x80004004 (E_ABORT))

我们有一个导出实用程序,可以将 Outlook 中的所有电子邮件导出到本地目录。 我们的工具工作得非常好。 但是现在我们正在迁移到 O365,从那时起我们就看到了该工具的问题。

从技术上讲,它可以读取所有电子邮件及其属性,如主题、发件人、收件人等,还可以保存或移动到 Outlook O365 中的其他文件夹。

但是我一执行 SAVEAS,就会收到错误“System.Runtime.InteropServices.COMException (0x80004004): Operation aborted (0x80004004 (E_ABORT))”。

下面是示例代码

        public static void ReadEmails()
        {
            try
            {
                Outlook.Application oApp = new();

                // Get the MAPI namespace.
                Outlook.NameSpace oNs = oApp.GetNamespace("MAPI");

                oNs.Logon("*****@*****.com", System.Reflection.Missing.Value,
                System.Reflection.Missing.Value, System.Reflection.Missing.Value);

                Outlook.Folders fols = oNs.Folders;

                Outlook.MAPIFolder inboxFolder = fols["****"].Folders["Inbox"];

                foreach (Outlook.Folder fol in inboxFolder.Folders)
                {
                    MessageBox.Show(fol.Name);
                    Outlook.Items items = fol.Items;  

                    foreach(Outlook.MailItem mailItem in items)
                    {
                        MessageBox.Show(mailItem.Subject);

                        try
                        {
                            //mailItem.Move(inboxFolder); -- this works
                            mailItem.SaveAs("test.msg", Outlook.OlSaveAsType.olMSG);
                        }
                        catch (System.Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }
                    }
                }                

                oNs.Logoff();
            }
            catch (System.Exception e)
            {
                Console.WriteLine("{0} Exception caught: ", e);
            }
        }

那么我需要做一些特别的事情吗?

仅供参考,该工具在用户笔记本电脑上作为用户在他自己的 email 帐户上执行。

代码看起来不错。 我看不出有什么奇怪的地方。 但以下异常可能表明存在多个问题:

System.Runtime.InteropServices.COMException (0x80004004): 操作中止 (0x80004004 (E_ABORT))

您很可能在 Outlook 中遇到过安全问题。 "Security" in this context refers to the so-called "object model guard" that triggers security prompts and blocks access to certain features in an effort to prevent malicious programs from harvesting email addresses from Outlook data and using Outlook to propagate viruses and spam. 这些问题或提示不能简单地关闭,除非在 Outlook 2007 中运行了防病毒应用程序。

以下策略可用于避免 Outlook 中的安全提示/问题:

  1. Outlook 基于的低级 API - 扩展 MAPI(或围绕该 API 的任何其他第三方包装器,例如,兑换)

  2. Outlook 安全管理器是一种编程工具,可让您抑制由与 Microsoft Outlook 2000 - 2013 交互的应用程序或加载项的代码调用的安全警报。

  3. 在公司环境中,管理员可以选择为部分或所有用户放松 Outlook 安全性。

  4. 开发一个受信任的 COM 插件并调用它来保存电子邮件,而不是直接使用 OOM。 该加载项可以访问不会触发安全问题的安全应用程序 object。

另一个可能的原因是 Microsoft 目前不推荐也不支持从任何无人值守的非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)自动化 Microsoft Office 应用程序,因为 Office 可能表现出不稳定Office 在此环境中运行时出现的行为和/或死锁。 以下是 MS 对此类情况的说明:

如果您正在构建在服务器端上下文中运行的解决方案,您应该尝试使用已确保无人值守执行安全的组件。 或者,您应该尝试找到允许至少部分代码在客户端运行的替代方案。 如果您使用来自服务器端解决方案的 Office 应用程序,该应用程序将缺少许多成功运行所需的功能。 此外,您将承担整体解决方案稳定性的风险。

Office 服务器端自动化的注意事项文章中阅读有关此内容的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM