0

Since last month serveral outlook installations Office 365 or Office 2016 have a problem with sending mail via interop. We create a mailitem via interop and then try to send the mail via mailitem.Send() Now we get a System.ArgumentException from Microsoft.Outlook.Interop.Outlook._MailItem.Send() We used this code since serveral years without any issues with different office versions.

Using mailItem.Display works as excepted. A workaround is to use mailItem.Display and mailItem.Send afterwards, but this is kind of ugly, because the window appears for a fraction of a second. Is this issue known and are there better solutions to solve this problem?

Additional informations: It doesn't matter if outlook is open or closed. We are using .NET 4.7.2, but the framework version shouldn't matter.

void TestMail(string to, string subject, string body, string attachment, bool useWorkaround = false)
{
    var outlook = new Microsoft.Office.Interop.Outlook.Application();
    var mail = outlook.CreateItem(OlItemType.olMailItem) as MailItem;
    Inspector oInspector = mail?.GetInspector; // https://stackoverflow.com/questions/11330101/can-only-send-email-via-outlook-if-outlook-is-open 
    if (mail == null)
        throw new Exception();
    mail.Subject = subject;
    mail.HTMLBody = body + mail.HTMLBody;
    Microsoft.Office.Interop.Outlook.Recipient recipient = mail.Recipients.Add(to);
    recipient.Type = (int)OlMailRecipientType.olTo;
    if (!mail.Recipients.ResolveAll())
        throw new Exception();
    if (!string.IsNullOrEmpty(attachment)
        mail.Attachments.Add(attachment);
    if (useWorkaround)
        mail.Display();
    mail.Send();
 }

3 Answers3

1

You are absolutely right, the .net framework version doesn't matter in that case. There are multiple reasons why it can happen in Outlook.

First of all, I'd suggest trying to use the Save method first and resolving recipients (ResolveAll or Resolve methods) before submitting an email. See non-helpful exception flagged on _MailItem.Send() for more information.

Second, you may try to switch off the security guard on the Outlook object model or just use a low-level API on which Outlook is based on to avoid security issues in Outlook. For example, the Outlook Security Manager component is a one-line programming tool that allows you to bypass security settings and avoid security warnings, alerts or prompts in add-ins and applications that interact with Microsoft Outlook. Also you may try to use Extended MAPI or any other wrappers around that API such as Redemption. The low-level code doesn't trigger security issues like OOM does.

It is not when and when the Send method is called and which code was used for creating that item.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thanks, for the hints. ResolveAll is already called and returns true. I tried to use Save before the call to Send but the same problem occurs. – Benny Bürger Apr 19 '21 at 12:39
  • Did you try to avoid security issues coming from the OOM when you automate Outlook from external applications? Did you try to use the `Send` method from an add-in or VBA macro? Do you get the same results? – Eugene Astafiev Apr 19 '21 at 12:51
  • The security guard is not the problem. I tried it by activating it by force and the popup occured but the error remained. I did not try it from within a plugin, yet. – Benny Bürger Apr 19 '21 at 13:45
  • What is your code? How do you add recipients and resolve them? – Eugene Astafiev Apr 19 '21 at 16:57
0

I got it working by removing the code

Inspector oInspector = mail?.GetInspector; // https://stackoverflow.com/questions/11330101/can-only-send-email-via-outlook-if-outlook-is-open 

I don't know why this line causes problems, but funnily enough the line now does the exact opposite of what it was intended to do.

0

I got it working by issuing myInspector.Close(olSave) before you do the Send. This works only when Outlook is open.