【发布时间】: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