简体   繁体   中英

Plain text to HTML conversion in Outlook email

I am facing a situation to preserve the formatting of plain text email when displaying it as virtual plain text in C sharp. This is done during receiving in Outlook 2007 using VSTO. The code below does not do the job, instead it converts the body into Times New Roman;Font Size 10 and displays it to the user.

string Text = "<html><body><p style=\"font-family:consolas;font-size:88%;\">" + mailItem.Body+ "</p></body></html>";

mailItem.HTMLBody = Text;

mailItem.HTMLBody = Regex.Replace(mailItem.HTMLBody, "(ASA[a-z][a-z][0-9][0-9])", "<a href=\"http://stack.com/eg=$&\">$&</a>");

How can I rectify this problem?

EDIT:

Input:

ASAss87

ASAjj98

this is test input

Output:

ASAss87 ASAjj98 this is test input

EDIT 2:

Input:

ASAss87

ASAjj98

this is test input.

Output:

ASAss87

ASAjj98

   this is test input.

*Moves one or two spaces forward. I am using tag.

Based on your feedback in the comments, try changing your first line to use Body instead of HTMLBody :

string Text = "<html><body><p style=\"font-family:consolas;font-size:88%;\">" + mailItem.Body+ "</p></body></html>";

Edit: Since the plain text contains line-breaks, maybe you should use a <pre> tag instead of a <p > tag, to prevent it from putting everything on one line.

string Text = "<html><body><pre style=\"font-family:consolas;font-size:88%;\">" + mailItem.Body+ "</pre></body></html>";

Edit2: Alternatively, you can replace all line-breaks with <br> tags.

string Text = "<html><body><p style=\"font-family:consolas;font-size:88%;\">" + mailItem.Body.Replace(Environment.NewLine,"<BR>") + "</p></body></html>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM