简体   繁体   English

Outlook 互操作 System.ArgumentException 发送但未显示

[英]Outlook interop System.ArgumentException on send but not on display

Since last month serveral outlook installations Office 365 or Office 2016 have a problem with sending mail via interop.自上个月以来,多个 outlook 安装 Office 365 或 Office 2016 在通过互操作发送邮件时出现问题。 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.我们通过互操作创建一个 mailitem,然后尝试通过 mailitem.Send() 发送邮件 现在我们从 Microsoft.Outlook.Interop.Outlook._MailItem.Send() 得到一个 System.ArgumentException具有不同的办公版本。

Using mailItem.Display works as excepted.使用 mailItem.Display 可以例外。 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.一种解决方法是在之后使用 mailItem.Display 和 mailItem.Send,但这有点难看,因为 window 会出现几分之一秒。 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.附加信息: outlook 是打开还是关闭都没有关系。 We are using .NET 4.7.2, but the framework version shouldn't matter.我们使用的是 .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();
 }

You are absolutely right, the .net framework version doesn't matter in that case.你是绝对正确的,在这种情况下,.net 框架版本无关紧要。 There are multiple reasons why it can happen in Outlook.在 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.首先,我建议在提交 email 之前先尝试使用Save方法并解析收件人( ResolveAllResolve方法)。 See non-helpful exception flagged on _MailItem.Send() for more information.有关详细信息,请参阅_MailItem.Send() 上标记的无用异常

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. 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.例如, Outlook 安全管理器组件是一种单行编程工具,可让您绕过安全设置并避免与 Microsoft Outlook 交互的加载项和应用程序中的安全警告、警报或提示。 Also you may try to use Extended MAPI or any other wrappers around that API such as Redemption.您也可以尝试使用扩展 MAPI 或围绕该 API 的任何其他包装器,例如 Redemption。 The low-level code doesn't trigger security issues like OOM does.低级代码不会像 OOM 那样触发安全问题。

It is not when and when the Send method is called and which code was used for creating that item.这与调用Send方法的时间和时间以及用于创建该项目的代码无关。

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.我不知道为什么这条线会导致问题,但有趣的是,这条线现在与它的意图完全相反。

I got it working by issuing myInspector.Close(olSave) before you do the Send.在您执行发送之前,我通过发出 myInspector.Close(olSave) 使其工作。 This works only when Outlook is open.这仅在 Outlook 打开时有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM