【发布时间】:2021-07-07 16:38:10
【问题描述】:
自上个月以来,多个 Outlook 安装 Office 365 或 Office 2016 在通过互操作发送邮件时出现问题。 我们通过互操作创建一个 mailitem,然后尝试通过 mailitem.Send() 发送邮件 现在我们从 Microsoft.Outlook.Interop.Outlook._MailItem.Send() 得到一个 System.ArgumentException 我们从几年前开始使用此代码,在不同的 Office 版本中没有任何问题。
使用 mailItem.Display 作为例外。 一种解决方法是在之后使用 mailItem.Display 和 mailItem.Send,但这有点难看,因为窗口会出现几分之一秒。 这个问题是否已知,是否有更好的解决方案来解决这个问题?
附加信息: 无论前景是开放的还是关闭的。 我们使用的是 .NET 4.7.2,但框架版本无关紧要。
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();
}
【问题讨论】: