Here’s a really odd problem I got the other day.

The problem

An web application I have been maintaining sent some mail to people in different countries, hence the mails contained characters like ä, é, č and so on and so forth.

Sending and receiving the mails was no problem at all. However, when some users tried to forward the emails using their mail client (Microsoft Outlook in this case) strange things would indeed happen.

Outlook would (unintentionally) insert a lot of random bytes at the start of the forwarded message, so it would look something like this :

厣叵卬向 … etc etc.

Dear Customer etc etc.

 

I had a single ascx (ASP.NET web control) for each mail in each language. I would then use LoadControl(..) to render the correct mail as a string, and then do a few string.Replace(..)’s to insert various data into the mail-strings. I did not use plain HTML files for the mail “templates” because the ASP.NET process ran in medium trust which prevents file IO, and I didn’t want another dependency on special config for making this work.

The solution 

The whole issue was of course caused by an encoding error.

To solve the problem I had to do a couple of things for each of the .ascx files:

  • The <head> section had to contain a <meta content=”text/html; charset=utf-8″ http-equiv=”Content-Type”/> tag.
  • The .ascx file itself had to be saved as signed UTF-8 in Visual Studio: File->Advanced Save Options and choose Encoding
  • Finally, the Encoding property had to be set UTF-8 on the SmtpClient object used to send the mails.

When the mails are read as strings internally in .NET they are stored in UTF-16 which is fine. When they are sent they are sent as UTF-8, and this is also doubly confirmed by the meta tag and also by the three signature bytes at the start of the mail’s HTML body (remember the mails were stored as signed UTF-8). So this solved the problem – no more accidental chinese letters at the start of the mails!

A Poem, by Microsoft Outlook 2007

Outlook inserted quite a few chinese letters at the start of the badly encoded mails. So what did they mean? Well, the wonder of Google’s translation engine gave me the following little gem:

Dan Shen Yu

Say nothing of Jiuxi Bian Wei Xun

Jiaoqianqixiao Xifangyunjie

King Wu Chung

“tick”

luster of gems tumultuous

an opposite wave of A Brighter Summer Day

Ke Xiatiguke retention

Tally Wei-Qiang Dong Mi

Mi Mi coconut coconut soup

Mei drive industry

Jiu Zhi Tang

Advertisements