Outlook Interop Throws Error "Exception from HRESULT: 0x80004004 (E_ABORT)" When Sending Email

1

I am trying to send an automated email using a C# console app. I am using the Outlook Interop to do this. Outlook is open when I run the code below. The code actually produces the email, which is sitting open when the code stops on mailItem.Send(). I suspect the issue has to do with some access policy on my work laptop, but any guidance would be helpful.

    public static void SendEmail(string Body, string Subject, string Recipients)
    {
        Outlook.Application app = new Outlook.Application();
        Outlook.MailItem mailItem = app.CreateItem(Outlook.OlItemType.olMailItem);
        mailItem.Subject = Subject;
        mailItem.To = Recipients;
        mailItem.HTMLBody = Body;
        mailItem.Display(false);
        mailItem.Send();
    }

Full Error:

 System.Runtime.InteropServices.COMException: 'Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))'
c#
email
outlook
interop
asked on Stack Overflow Oct 11, 2017 by DanG

2 Answers

1

Looks like you have faced with Outlook security issue. "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.

There are some ways to avoid such issues:

  1. Use a low-level API (Extended MAPI) which doesn't trigger security issues or prompts. Or just any other third-party wrapper around that API such as Redemption.
  2. Develop a COM add-in and use the safe Application object passed as a parameter to one of the event handler.
  3. Use the Outlook Security Manager component for suppressing security checks at runtime.

Read more about all these options and other ways in the Outlook "Object Model Guard" Security Issues for Developers article.

answered on Stack Overflow Oct 11, 2017 by Eugene Astafiev
0

The code does not really make much sense - you are displaying the message modelessly, so Display returns immediately, and then you call Send before the user has a chance to see anything on the screen. Do you want the user to click the Send button? In that case, just call Display, and let the user do the rest - there is no reason to call Send.


User contributions licensed under CC BY-SA 3.0