Encoding of email subject lines - garbled international characters


My website (IIS6, ASP.net, VB.net) has to send out several email messages, some in html, some in text, and the encoding of international characters works well throughout the site. However, the subject lines of emails being sent refuse to accept the encoding provided.

Example: Appétit in the source shows up properly as Appétit's in the html (even in the page title) and the body of the email, but not in the subject line of an email.

Why is that and what can I do? I have tried several globalization settings in the web.config and also looked into encoding properties of the mailmessage class - but there's only bodyEncoding, so I didn't experiment there.

Who is Participating?

Improve company productivity with a Business Account.Sign Up

AerosSagaConnect With a Mentor Commented:
heres your answer:


 3.1 How do I send non US-ASCII emails? Printer Friendly   Email This FAQ   Discuss
In the beginning, when email was first being used, it was all us-ascii content. To handle different languages and character sets, different encodings must be used. The following example demonstrates sending a non us-ascii email, using the Chinese GB2312 character set as an example. The hardest part of sending non us-ascii email, is to determine the correct character set. For reference, an easy to use character set chart can be found at aspNetEmail's website, here: http://www.aspnetemail.com/charsets.aspx .

Note: There is one major pitfall with this technique. The original email rfcs required that all non us-character sets use the quoted-printable or base64 content transfer encoding (convert all characters above 127, to 2 or more bytes, so the individual byte value is less than 128). Email readers, then take these bytes, and convert them back to the original character set. System.Web.Mail does not encode higher end character sets. Instead, it just sends the characters unencoded (commonly known in the email world as 8 bit encoding). Thus, if your email recipient's mail server, or gateway, does not support 8 bit encoding, they will not receive the email. THERE IS NO WAY PROGRAMMATIC WAY OF CHECKING FOR THIS. The only way, is make sure the email message is properly encoded using the quoted-printable (preferred) or base64 content transfer encoding. If you are sending a newsletter, this can be a huge problem, because your newsletter will simply not be delivered, and there is a good chance, you will never know it. As an email vender, I've had customer after customer encounter this problem. And it has always been fixed by using the quoted-printable format. Because this cannot be controlled using System.Web.Mail, you will need to use a third party product like aspNetEmail. The bottom line is this: if you need to send emails, using a higher end character set, DO NOT USE System.Web.Mail. Either roll your own component (if you have the time), or purchase a third party product that properly encodes email messages.

For those people that don't have a choice, and have to use System.Web.Mail, the following is a code snippet demonstrating sending a non us-ascii email.
[ C# ]

MailMessage mail = new MailMessage();
mail.To = "me@mycompany.com";
mail.From = "you@yourcompany.com";
mail.Subject = "this is a test email.";
mail.Body = "Some Chinese characters or text goes here";
mail.BodyEncoding = System.Text.Encoding.GetEncoding( "GB2312" ); //set the proper character set here
SmtpMail.SmtpServer = "localhost";  //your real server goes here
SmtpMail.Send( mail );

[ VB.NET ]

Dim mail As New MailMessage()
mail.To = "me@mycompany.com"
mail.From = "you@yourcompany.com"
mail.Subject = "this is a test email."
mail.Body = "Some Chinese characters or text goes here"
mail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312") 'set the proper character set here
SmtpMail.SmtpServer = "localhost" 'your real server goes here


ddeteringAuthor Commented:
Thank you, Aeros. I have tried your code snippets (just to make sure) and set the mail.bodyencoding to several options, but the subject line remained flawed. Are you saying that with AspNetEmail I will be able to send other characters from my application? (I know that GroupMail can - encoding "western european / americas").
Your referencing the ones in your web.config here?

System.Text.Encoding.GetEncoding("GB2312") 'set the proper character set here
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

ddeteringAuthor Commented:
Not sure whether I understand your question. I set the encoding in the web.config:
 <globalization requestEncoding="ISO-8859-1" responseEncoding="ISO-8859-1" />
 and assume that it will be the default one for mailmessage.bodyencoding. But then I have also set mailmessage.bodyencoding to UTF-8 and ASCII. None yielded any different result.
check into setting your response encoding in your web.config:

Visual Basic and Visual C# Concepts       
Editing the Web.config File for Web Forms Globalization

Web Forms configuration files, named Web.config, provide settings for every Web Forms page in the same directory as the configuration file. The settings are usually also inherited by subdirectories. Each file can contain a globalization section in which you can specify default encodings and cultures. Values are valid if they are accepted by the related Encoding and CultureInfo classes. For more information on the contents of the Globalization section, see <globalization> Element. For general information on Web.config files, see ASP.NET Configuration.

The Web.config file is case-sensitive, so specifying a value for "FileEncoding" will not have the desired effect, but specifying a value for "fileEncoding" will. For more information, see Format of ASP.NET Configuration Files.

To edit the Web.config file

    * Set the values of fileEncoding, requestEncoding and responseEncoding to specify the way in which .aspx files, request data and response data are encoded. For more information on encoding values, see Selecting an Encoding for Web Forms Globalization

      If the file encoding is specified in the Web.config file, the actual file must be saved in the same encoding. To select an encoding different from the system default, use the Advanced Save Options dialog box (available on the File menu).
    * Set the values of culture and uiCulture. For more information on culture values, see Setting the Culture and UI Culture for Web Forms Globalization.


          Note   The attributes of the globalization section in the Web.config file can also be specified in the @ Page directive (with the exception of fileEncoding, which can only be specified in Web.config because it applies to the file itself). Settings in the Page directive are only valid for a specific page and override the settings of the Web.config file.


Thats really the only other thing I can think of

ddeteringAuthor Commented:
I can't get it work with the system.web.mail class. Just have sent an inquiry to aspnetmail whether their product does it.
ddeteringAuthor Commented:
Okay, based on their answer, aspnetmail wouldn't have any problems with this issue and it seems that the system.web.mail class cannot encode any non-us ascii characters in the subject line. This is the short but comprehensive answer. Aeros had this buried somewhere in his first answer, so s/he's getting the points.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.