【问题标题】:Send an email with Outlook without a subject --- dialog box issue使用 Outlook 发送没有主题的电子邮件 --- 对话框问题
【发布时间】:2019-08-19 05:22:00
【问题描述】:

我正在使用以下代码通过 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();
    }

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

我想跳过此提示或自动接受(仍然发送)。我如何做到这一点?

【问题讨论】:

  • app.DisplayAlerts = false 是在自动化过程中阻止其他 Office 应用程序显示对话框的方法,但不知道这是否适用于 Outlook。可能值得一试。
  • 您使用这种方式而不是 SMTP 是否有任何具体原因?
  • @stuartd - Outlook.Application 对象不公开 DisplayAlerts 属性。
  • @DmitryStreblechenko 我认为它可能不会。顺便说一句,多年前我是一个非常快乐的 Redemption 用户 :)
  • @WamiqRehman 是的,我有理由使用Microsoft.Office.Interop.Outlook 而不是System.Net.Mail。

标签: c# outlook office-interop


【解决方案1】:

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

【讨论】:

  • 这是正确的解决方案---我误解了mailItem.Display(false) 会做什么,我认为我需要它来防止对话框/窗口/等。从显示。结果恰恰相反!
【解决方案2】:
  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。有关受保护对象模型入口点的更多信息,请参阅Protected Properties and Methods。

为了避免安全提示,您可以使用:

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

【讨论】:

    猜你喜欢
    • 2017-12-26
    • 2019-06-15
    • 2011-11-22
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 2012-01-02
    相关资源
    最近更新 更多