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

Viewed 13

We have a Export Utility which exports all emails from Outlook to local directory. And our tools work perfectly fine. But now we are migrating to O365 and since then we are seeing issues with the tool.

Technically the does all the things like able to read all emails and its properties like Subject, From, To etc and also able to SAVE or MOVE to other folder within Outlook O365.

But I get an error "System.Runtime.InteropServices.COMException (0x80004004): Operation aborted (0x80004004 (E_ABORT))" as soon I execute SAVEAS.

Below is the sample code

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

So do I have to do anything special?

FYI, the tool gets executed on user laptop as a user on his own email account.

1 Answers

The code looks good. I don't see anything strange there. But the following exception may indicate multiple issues:

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

Most probably you have faced with a security issue in 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. These issues or prompts cannot simply be turned off, except in Outlook 2007 with an anti-virus application running.

The following strategies can be used for avoiding the security prompts/issues in Outlook:

  1. A low-level API on which Outlook is based on - Extended MAPI (or any other third-party wrappers around that API, for example, Redemption).

  2. Outlook Security Manager is a programming tool that allows you to suppress security alerts invoked by the code of your application or add-in that interacts with Microsoft Outlook 2000 - 2013.

  3. In a corporate environment, the administrator may choose to loosen Outlook security for some or all users.

  4. Develop a trusted COM add-in and call it for saving emails instead of using OOM directly. The add-in has access to a secure Application object which doesn't trigger security issues.

Another possible cause is that Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment. Here is what MS states for such cases:

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.

Read more about that in the Considerations for server-side Automation of Office article.

Related