none
[OL 2013] Set Charset of MailItem while composing RRS feed

  • Question

  • Hi,

    i'm trying to edit an email before sending it (send event). There are three steps i need to do:

    1. Change the encoding of the mail to utf-8
    2. Read the HTMLBody or Body (will be processed to another text)
    3. Change to OlFormatPlain and write the changed Body property

    Well, the second and third step are working for me, but my charset is always resetting to ISO-8859-1 (or what the user has configured in his settings).

    Here is a code snippet:

    private void ChangeMailCharsetToUtf8(ref MailItem mail)
    {
    	if (mail.InternetCodepage == System.Text.Encoding.UTF8.CodePage)
    		return;
    
    	string sBody = null;
    
    	if (mail.BodyFormat == OlBodyFormat.olFormatPlain)
    		sBody = mail.Body;
    	else if (mail.BodyFormat == OlBodyFormat.olFormatHTML)
    		sBody = mail.HTMLBody;
    
    	mail.InternetCodepage = System.Text.Encoding.UTF8.CodePage;
    
    	if (mail.BodyFormat == OlBodyFormat.olFormatPlain)
    		mail.Body = sBody;
    	else if (mail.BodyFormat == OlBodyFormat.olFormatHTML)
    		mail.HTMLBody = sBody;
    }

    FYI: If the (HTML)Body is not set after changing the InternetCodepage, non-ascii characters are broken.

    Okay, now to my debugger results:

    The MailItem accepts the change of the codepage, but when setting the (HTML)Body back to its real value, the InternetCodepage resets itself. In Outlook 2010 it works fine.
    The HTMLBody did not contain any <meta> values describing the charset.

    Does anyone know, what is going on there?

    Thanks,
    Daniel

    Monday, February 4, 2013 3:46 PM

Answers

  • I found the damn registry key :) Thanks to Process Monitor

    HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Outlook\Options\MSHTML\International!Autodetect_CodePageOut = dword:0

    • Marked as answer by Ranfty2000 Tuesday, February 12, 2013 5:01 PM
    Tuesday, February 12, 2013 5:00 PM

All replies

  • Why not explicitly specify the charset in the HTML body?

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

    Monday, February 4, 2013 4:27 PM
  • Thanks for this hint, i will try it today. Results follow...

    Tuesday, February 5, 2013 7:31 AM
  • Doesn't work :( But if anyone needs the code - here it is:

    public static string SetHtmlEncoding(string htmlBody, string encodingName)
    {
    	int startEncoding, length;
    	Regex reg = new Regex(
    		@"<meta\s+http-equiv\s*=\s*""\s*content-type\s*""\s+content\s*=\s*""([^;]+(?:;\s*charset\s*=\s*([^ ""]+))?\s*)?""\s*/?\s*>",
    		RegexOptions.IgnoreCase | RegexOptions.Singleline);
    	Match match = reg.Match(htmlBody);
    	if (match.Success && match.Groups[2].Success)
    	{
    		startEncoding = match.Groups[2].Index;
    		length = match.Captures[2].Length;
    		htmlBody = htmlBody.Remove(startEncoding, length);
    		htmlBody = htmlBody.Insert(startEncoding, encodingName);
    	}
    	else if (match.Success && match.Groups[1].Success)
    	{
    		startEncoding = match.Groups[1].Index;
    		length = match.Groups[1].Length;
    		htmlBody = htmlBody.Insert(startEncoding + length, "; charset=" + encodingName);
    	}
    	else
    	{
    		startEncoding = htmlBody.ToLower().IndexOf("</head>");
    		htmlBody = htmlBody.Insert(startEncoding, "<meta http-equiv=\"content-type\" content=\"text/html;charset=" + encodingName + "\">");
    	}
    	return htmlBody;
    }
    
    Any other ideas?

    Tuesday, February 12, 2013 3:52 PM
  • I found the damn registry key :) Thanks to Process Monitor

    HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Outlook\Options\MSHTML\International!Autodetect_CodePageOut = dword:0

    • Marked as answer by Ranfty2000 Tuesday, February 12, 2013 5:01 PM
    Tuesday, February 12, 2013 5:00 PM