Search code examples
c#visual-studio-2010outlook-addinoutlook-2010

Adding text to email.Body messes with the text formatting


I'm trying to replace some text in the body of the email, but whenever I do it seems to screw up the formatting of the signature text. For example, if I try email.Body.Replace("Info", "Information") then this text:

First Last
Title

Info
Website: www.stackoverflow.com

turns into:

First Last
Title

Information
Website: HYPERLINK "http://www.stackoverflow.com/"www.stackoverflow.com

This happens even with just a simple addition to the text like email.Body += "text".

Along with this, it also switches the font of the body from Arial 10 to Calabri 12 (this probably has to do with Calabri 12 being the default email text when Arial 10 is the signiture font).

How would I save the format of the current email body so that when I add text to it, I could set it back or what would be the best way to add text to the body to keep the formatting?

Your help would be welcomed.


Solution

  • MailMessage.Body can be formatted in RTF, which I think it does by default, or in HTML. What the top message you posted actually looks like is something like this:

    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
    {\colortbl ;\red0\green0\blue255;}
    {\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sl240\slmult1\lang9\f0\fs22 First Last \par
    Title \par
    \par
    Info \par
    Website: {\field{\*\fldinst{HYPERLINK "www.stackoverflow.com"}}{\fldrslt{\ul\cf1 www.stackoverflow.com}}}\f0\fs22\par
    }
    

    I'm assuming that appending "text" to the end of that is causing the RTF message to be parsed improperly, resulting in a loss of formatting.

    http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.body.aspx