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