Search code examples
c#-4.0html-emailoutlook-2010

Outlook does not render html content


I have following email message.

enter image description here

after running the following code

   string pattern = "<img src=\"cid.*?</span></p>|Inline image 1.*?</FONT>";

        Outlook.Selection mySelection = Globals.ThisAddIn.Application.ActiveExplorer().Selection;
        Outlook.MailItem mailitem = null;

        foreach (Object obj in mySelection)
        {
            if (obj is Outlook.MailItem)
            {
                mailitem = (Outlook.MailItem)obj;                                    
                string body = mailitem.HTMLBody;
                Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.Multiline|RegexOptions.Singleline);
                MatchCollection matchs = reg.Matches(body);
                    foreach(Match match in matchs)
                    {
                        string a = match.Groups[0].Value;
                        mailitem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
                        mailitem.Body = body.Replace(a, string.Empty);                            
                        mailitem.Save();
                    }
                    //mailitem.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;
            }
        }

I got following email message in outlook . enter image description here

The body text works in browser. It means when i saved the body text in simple html file it works properly and display original message.


Solution

  • You have to change body format string.

     foreach(Match match in matchs)
                    {
                        string a = match.Groups[0].Value;
                        mailitem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
                        mailitem.HTMLBody = body.Replace(a, string.Empty);                            
                        mailitem.Save();
                    }