Search code examples
c#outlookoffice-interop

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


I am using the following code to send emails using 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();
    }

It works almost perfectly, but I run into a problem when trying to send an email without a subject. The following dialog box pops up:

enter image description here

I want to skip this prompt or automatically accept (send anyway). How do I achieve this?


Solution

  • Comment out the mailItem.Display(false); line.