Search code examples
stringmailmessagerebex

Manipulate Rebex.MailMessage Html String


I am currently working on a message watcher service we have and my task is to simply embed message details into the message body.

I have tried using a string builder for this however i have found that my message body is a html based string.

I'm wanting to know if there is a way i can add the values i want to add at a certain point in this html string?

Below is a section of the html string i want to manipulate. My text needs to be inserted directly after the body tag.

 <body lang=EN-GB link=blue vlink=purple>
   <div class=WordSection1>
     <p class=MsoNormal>
       <span style='font-size:10.0pt;font-family:"Century Gothic","sans-serif";color:black'>Another test for AppendLine();<o:p></o:p>
       </span>
     </p>

Here is how i was trying to do it:

 StringBuilder sb = new StringBuilder();

            sb.Append("From: ");
            sb.Append(message.From.ToString());
            sb.AppendLine();
            sb.Append("Sent: ");
            sb.Append(message.Date.ToString());
            sb.AppendLine();
            sb.Append("To: ");
            sb.Append(message.To.ToString());
            sb.AppendLine();
            sb.Append("Subject: ");
            sb.Append(message.Subject);
            sb.AppendLine();
            sb.Append(message.BodyHtml);

Unfortunately this just printed my From, Sent, To, Subject values onto one line and then output the html section.

If any more information is needed please let me know and i will provide it.


Solution

  • Instead of appending the body HTML, try this regex replace:

    StringBuilder sb = new StringBuilder();
    
    sb.Append("From: ");
    sb.Append(message.From.ToString());
    ...
    sb.Append("Subject: ");
    sb.Append(message.Subject);
    sb.AppendLine();
    
    // not guaranteed to work with arbitrary HTML strings
    Regex regex = new Regex(@"(<body[^>]*>)", RegexOptions.IgnoreCase);
    message.BodyHtml = regex.Replace(message.BodyHtml, "$1\r\n" + sb.ToString());
    

    Disclaimer: Please be advised that processing HTML with regular expressions is generally regarded as a bad idea. Although the code above might work in 98% of cases, regular expressions are not up to the task of parsing arbitrary HTML. HTML is too sophisticated for regex. If you need to process arbitrary HTML bodies (not just the one above), I strongly recommend using a proper HTML parser such as HTML Agility Pack - even for seemingly-simple operations such as inserting a text after the body tag.