When composing a new Mail (using Outlook 365) the HTML body should be manipulated before the MailItem opens to the user. To avoid security warnings I use SafeMailItem
to read the HTMLBody
property:
Application outlook = new Application();
MailItem mailItem = outlook.CreateItem(OlItemType.olMailItem);
inspector = mailItem.GetInspector;
SafeMailItem safeMailItem = new SafeMailItem();
safeMailItem.Item = mailItem;
Console.WriteLine("HTMLBody (SafeMailItem): " + Environment.NewLine + safeMailItem.HTMLBody);
Console.WriteLine("HTMLBody (MailItem): " + Environment.NewLine + mailItem.HTMLBody);
// Manipulate HTML body
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(safeMailItem.HTMLBody); // Avoid security warning
HtmlNode firstParagraph = doc.DocumentNode.SelectSingleNode("/html/body//p");
firstParagraph.FirstChild.InnerHtml = "My custom text";
mailItem.HTMLBody = doc.DocumentNode.WriteContentTo(); // Change HTML body of email
mailItem.Display(false);
// Listen to close/send
((InspectorEvents_10_Event)mailItem.GetInspector).Close += MailItem_Close;
outlook.ItemSend += Outlook_ItemSend;
Output is:
HTMLBody (SaveMailItem):
<HTML>
<HEAD><META http-equiv=Content-Type content="text/html; charset=UTF-8"></HEAD>
<BODY>
<!-- Converted from text/plain format -->
</BODY></HTML>
HTMLBody (MailItem):
<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta name=ProgId content=Word.Document><meta name=Generator content="Microsoft Word 15"><meta name=Originator content="Microsoft Word 15"><link rel=File-List href="cid:filelist.xml@01D64880.436A9300"><!--[if gte mso 9]><xml>
<o:OfficeDocumentSettings>
<o:AllowPNG/>
</o:OfficeDocumentSettings>
</xml><![endif]--><link rel=themeData href="~~themedata~~"><link rel=colorSchemeMapping href="~~colorschememapping~~"><!--[if gte mso 9]><xml>
<w:WordDocument>
<w:TrackMoves/>
<w:TrackFormatting/>
<!-- ... -->
Why is there a difference? Is there a better approach to 1) create an email 2) manipulate the HTML body and 3) display the email to the user?
I finally found the solution.
After calling mailItem.GetInspector
the MailItem
has to be saved (MailItem.Save()
). So the complete working code is:
Application outlook = new Application();
MailItem mailItem = outlook.CreateItem(OlItemType.olMailItem);
inspector = mailItem.GetInspector;
mailItem.Save(); // Save mailItem to ensure that HTMLBody can be access from SafeMailItem
SafeMailItem safeMailItem = new SafeMailItem();
safeMailItem.Item = mailItem;
Console.WriteLine("HTMLBody (SafeMailItem): " + Environment.NewLine + safeMailItem.HTMLBody);
Console.WriteLine("HTMLBody (MailItem): " + Environment.NewLine + mailItem.HTMLBody);
// Manipulate HTML body
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(safeMailItem.HTMLBody); // Avoid security warning
HtmlNode firstParagraph = doc.DocumentNode.SelectSingleNode("/html/body//p");
firstParagraph.FirstChild.InnerHtml = "My custom text";
mailItem.HTMLBody = doc.DocumentNode.WriteContentTo(); // Change HTML body of email
mailItem.Display(false);
// Listen to close/send
((InspectorEvents_10_Event)mailItem.GetInspector).Close += MailItem_Close;
outlook.ItemSend += Outlook_ItemSend;