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

Leon Zhou 71 Reputation points
2022-08-12T00:01:55.797+00:00

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.

Does anyone know where I can report this bug to Microsoft?

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,562 questions
Outlook Management
Outlook Management
Outlook: A family of Microsoft email and calendar products.Management: The act or process of organizing, handling, directing or controlling something.
4,931 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Oskar Shon 866 Reputation points MVP
    2022-11-08T17:16:06.893+00:00

    You tape line with:

    mail.HTMLBody = mail.Body;  
    

    You should use

    mail.HTMLBody = "your html <b>code</b>"  
    
    0 comments No comments

  2. Fayez Moussa 1 Reputation point
    2022-11-08T23:59:45.397+00:00

    I am having a similar problem - accessing the email using the Interop.Outlook library. We have an application the processes the email and appends a message to the bottom of the email. Just recently, quite a few users who are on the latest version of Outlook are reporting that some emails, after being processed, get garbled chinese characters.

    Stepping through the debugger, it occurs the moment I either update HTMLBody of the email with additional text (we are ensuring that the html is valid) or create a copy of the email. This application has been working for the last 10 years without any changes.

    _mail.HTMLBody = newHTmlBody;  
    

    Any idea on how to fix this?

    0 comments No comments

  3. Oskar Shon 866 Reputation points MVP
    2022-11-09T08:18:37.427+00:00

    That is not a similar I think.
    If you have deferent characters in newHTmlBody, then you should change Encoding your file.
    In vb.net is simple, you should change System.IO.StreamWriter from Default to UTF8 or Unicode
    In VBA you should find right function to change Encoding
    or you should change format your source file.

    Regards

    0 comments No comments