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))'
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:
Read more about all these options and other ways in the Outlook "Object Model Guard" Security Issues for Developers article.
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