使用没有主题的 Outlook 发送电子邮件 --- 对话框问题

Send an email with Outlook without a subject --- dialog box issue

我正在使用以下代码通过 Outlook 发送电子邮件:

    private static void SendMailItem(string from, string to, string subject, string body, string attachment = null)
    {
      Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
      Microsoft.Office.Interop.Outlook.MailItem mailItem = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
      mailItem.Subject = subject;
      mailItem.To = to;
      mailItem.Body = body;
      if (attachment != null)
      {
        mailItem.Attachments.Add(attachment, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue);
      }
      mailItem.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh;
      mailItem.Display(false);
      mailItem.Send();
    }

它工作得几乎完美,但是我 运行 在尝试发送没有主题的电子邮件 时遇到了问题 。弹出如下对话框:

我想跳过此提示或自动接受(无论如何发送)。我该如何实现?

注释掉 mailItem.Display(false); 行。

  1. 您可以指定 Subject 行。确保字符串不为空或 null。
  2. 或者只发送项目而不显示它:
private static void SendMailItem(string from, string to, string subject, string body, string attachment = null)
   {
     Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
     Microsoft.Office.Interop.Outlook.MailItem mailItem = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
     mailItem.Subject = subject;
     mailItem.To = to;
     mailItem.Body = body;
     if (attachment != null)
     {
       mailItem.Attachments.Add(attachment, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue);
     }
     mailItem.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh;   
     mailItem.Send();
   }

请注意,您在自动化 Outlook 时可能会收到 Outlook 安全提示。 Outlook 对象模型包括访问 Outlook 数据、将数据保存到指定位置和发送电子邮件的入口点。这些入口点可供合法和恶意应用程序开发人员使用。应用了 Outlook 电子邮件安全更新的 Outlook 98 和 Outlook 2000 版本以及从 Outlook 2000 SP2 开始的所有后续版本都使用对象模型保护来帮助保护用户。

当不受信任的应用程序试图使用对象模型获取电子邮件地址信息、在 Outlook 外部存储数据、执行某些操作和发送电子邮件时,对象模型防护会警告用户并提示用户进行确认。尽管 Object Model Guard 成功地识别和保护了这些入口点,但存在两个主要问题使得 Object Model Guard 相当不实用:

应用程序在早期版本的 Outlook 中调用对象模型防护的默认情况可能会导致对合法应用程序的过度安全提示。

COM 的局限性和 Windows 在识别调用对象模型保护的特定应用程序方面的局限性使得用户很难确定地响应安全提示。

有关对象模型保护的各种安全提示的详细信息,请参阅Outlook Object Model Security Warnings. For more information on the protected object model entry points, see Protected Properties and Methods

要避免出现安全提示,您可以使用:

  1. Security Manager for Microsoft Outlook 允许即时关闭安全提示。
  2. 不触发安全提示的低级 API(扩展 MAPI)。或者只是围绕 API 的任何包装,例如 Redemption.
  3. 部署 Outlook 安全设置(针对管理员)。