none
Byte Order Mark (BOM) characters at the start of Outlook 2010 email message RRS feed

  • Question

  • Hi,

    I am loading the contents of a HTML file into an Outlook message.
    The problem is when the message is loaded in Outlook 2010, some unwanted charatcers (  ) appear at the start of the email. message. After some searching I understood the characters are byte order mark (BOM) used to denote byte order of the file.

    This however loads correctly in Office 2003. There is no BOM characters at the start of email in Outlook 2003.

    The HTML file is in UTF8 format.
    Following is code used to load the contents of HTML file into Outlook message:

            //load the template from file
            AnsiString m_sTemplateDocPath = "C:\\Documents and Settings\\DEV1\\Desktop\\SUP6E4.html";
            int         iFileHandle;
            int         iFileLength;
            char      *pszBuffer;
            iFileHandle = FileOpen(m_sTemplateDocPath, fmOpenRead);
            iFileLength = FileSeek(iFileHandle,0,2);
            FileSeek(iFileHandle,0,0);
            pszBuffer = new char[iFileLength];
            FileRead(iFileHandle, pszBuffer, iFileLength);
            FileClose(iFileHandle);
            //set body format to HTML
           pMailItem->set_BodyFormat(olFormatHTML) ;
            WideString htmlBody (pszBuffer);
            //load the template into message body
            pMailItem->set_HTMLBody( htmlBody ) ;

    Any idea why these characters are appearing only for Outlook 2010 message and how to get rid of these?

    Thanks

    Friday, May 18, 2012 6:31 AM

Answers

  • It is your responsibility to remove the BOM characters from the HTML file.

    Files are generally 8 bit, hence having a BOM mark is normal.

    HTMLBody property on the other hand is always Unicode (UTF-16), hence no BOM mark is required or expected.

    It is also your reponsibility to convert the 8 bit data to a Unicode string using the retrieved BOM marker. Your code converts from 8 bit data (pszBuffer variable) to Unicode (htmlBody variable) using the default conversion that uses the current code page, but chances are the BOM marker is UTF-8, so you must explicitly call MultiByteToWideChar(CP_UTF8, ...) rather than rely on the WideString constructor. And if the BOM marker is UTF-16, your code will simply break - the correct solution would then be to simply remove the BOM marker and treat the rest of the data as a wide char array, not ANSI.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    • Marked as answer by ank_s Saturday, May 19, 2012 6:16 AM
    Friday, May 18, 2012 4:59 PM

All replies

  • Can you reproduce in Outlook 2007 as well?  Word has been used as the rendering engine since that version so I'm guessing it's mucking things up.  I'm not sure if you'd be able to prevent this behavior though.

    Eric Legault
    MVP (Outlook)
    About me...

    Friday, May 18, 2012 1:58 PM
    Moderator
  • It is your responsibility to remove the BOM characters from the HTML file.

    Files are generally 8 bit, hence having a BOM mark is normal.

    HTMLBody property on the other hand is always Unicode (UTF-16), hence no BOM mark is required or expected.

    It is also your reponsibility to convert the 8 bit data to a Unicode string using the retrieved BOM marker. Your code converts from 8 bit data (pszBuffer variable) to Unicode (htmlBody variable) using the default conversion that uses the current code page, but chances are the BOM marker is UTF-8, so you must explicitly call MultiByteToWideChar(CP_UTF8, ...) rather than rely on the WideString constructor. And if the BOM marker is UTF-16, your code will simply break - the correct solution would then be to simply remove the BOM marker and treat the rest of the data as a wide char array, not ANSI.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    • Marked as answer by ank_s Saturday, May 19, 2012 6:16 AM
    Friday, May 18, 2012 4:59 PM