Leon Zhou
Leon Zhou

Reputation: 635

Setting plain text email's body format to HTML using Microsoft Outlook Interop Library corrupts the email content

This seems to start happening out of no where since May this year to the desktop version of Outlook, almost looks like a bug from some recent update that Microsoft is rolling out.

So I have the following simple code to reproduce this problem:

var app = new Application();
var session = app.Session;
var mail = (MailItem)session.GetItemFromID("0000");

// mail.BodyFormat is olBodyFormat.Plain
// setting it to olFormatHTML corrupts the email body

mail.BodyFormat = OlBodyFormat.olFormatHTML;

// after setting the body format to olFormatHTML, the body format remains olBodyFormat.Plain

The email body text after trying to set the body format to HTML changes to incorrect Unicode characters.

The following code achieves the same result:

var app = new Application();
var session = app.Session;
var mail = (MailItem)session.GetItemFromID("00000");
mail.HTMLBody = mail.Body;

If the plain text body was "asdf" and the email body encoding is Western European (Windows), i.e. InternetCodepage = 20127, it converts the text to "獡晤".

At first I thought this could be a page encoding problem so I tried the following code:

var app = new Application();
var session = app.Session;
var mail = (MailItem)session.GetItemFromID("00000");
mail.InternetCodepage = 65001; // sets the email encoding to UTF-8
mail.HTMLBody = mail.Body;

Yet the same thing still happens.

It seems that if I use Redemption to do this, the problem doesn't happen, but there are side effects to do this with Redemption.

After Redemption updates the email body, the Outlook inspector or the email window doesn't change the body format, as if Outlook still caches the old value. If you reply this email now, the sent email will have an empty body.

Does any one know if I'm doing anything wrong here or this is really a Microsoft Outlook bug?

Upvotes: 0

Views: 855

Answers (1)

Eugene Astafiev
Eugene Astafiev

Reputation: 49455

Looks like this is a product issue. I'd recommend opening a support case with MS then.

As a possible workaround you can use the Word editor for setting up the message body correctly. The Inspector.WordEditor property returns the Microsoft Word Document Object Model of the message being displayed.

Upvotes: 1

Related Questions