1

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.

4

3 回答 3

1

您必须更改正文格式字符串。

 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();
                }
于 2013-09-05T07:27:28.817 回答
1

请试试这个,[\n\r 和 < /br> 标签在这里不起作用,需要使用 <br> ]

示例代码:

    public void OpenOutlook()
    {
        try
        {

            Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
            Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

            oMsg.Subject = "emailSubject";
            oMsg.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
            oMsg.BCC = "emailBcc";
            oMsg.To = "emailRecipient";

            string body = "emailMessage";
            //if body contains \n\r replace that into <br>
            body = body.Replace("\r\n", "<br>");
            body = body.Replace("\n", "<br>");

            oMsg.HTMLBody = body;

            oMsg.Display(true); 
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
于 2016-08-11T11:07:39.537 回答
-2
mailitem.HTMLBody = body;

will do the trick.

于 2014-09-30T18:49:02.500 回答