2013-09-05 6 views
1

J'ai un message électronique suivant.Outlook ne rend pas le contenu html

enter image description here

après avoir exécuté le code suivant

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

Je suis message d'e-mail dans Outlook. enter image description here

Le texte du corps fonctionne dans le navigateur. Cela signifie que lorsque j'ai sauvé le texte du corps dans un fichier html simple, cela fonctionne correctement et affiche le message original.

Répondre

1

Vous devez changer la chaîne de format du corps.

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
mailitem.HTMLBody = body; 

fera l'affaire.

1

S'il vous plaît essayer [\ n \ r et </br> Les tags sont ne fonctionnera pas ici insted de ce besoin d'utiliser < br>]

Exemple Code:

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