【问题标题】:Outlook does not render html contentOutlook 不呈现 html 内容
【发布时间】:2013-09-08 18:18:51
【问题描述】:

我有以下电子邮件。

运行以下代码后

   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;
            }
        }

我在 Outlook 中收到以下电子邮件。

正文在浏览器中有效。这意味着当我将正文文本保存在简单的 html 文件中时,它可以正常工作并显示原始消息。

【问题讨论】:

    标签: c#-4.0 html-email outlook-2010


    【解决方案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();
                    }
    

    【讨论】:

      【解决方案2】:

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

      示例代码:

          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;
              }
          }
      

      【讨论】:

        【解决方案3】:
        mailitem.HTMLBody = body;
        

        会成功的。

        【讨论】:

          猜你喜欢
          • 2020-07-26
          • 1970-01-01
          • 2013-11-29
          • 1970-01-01
          • 1970-01-01
          • 2012-04-21
          • 1970-01-01
          • 2019-11-07
          • 1970-01-01
          相关资源
          最近更新 更多