Solved

Encoding of email subject lines - garbled international characters

Posted on 2004-09-28
7
1,506 Views
Last Modified: 2008-01-09
Hi!

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.

Thanks!
0
Comment
Question by:ddetering
  • 4
  • 3
7 Comments
 
LVL 17

Accepted Solution

by:
AerosSaga earned 125 total points
Comment Utility
heres your answer:

http://www.systemwebmail.com/faq/3.1.aspx

 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
SmtpMail.Send(mail)

Regards,

Aeros
0
 
LVL 1

Author Comment

by:ddetering
Comment Utility
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").
0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
Your referencing the ones in your web.config here?

System.Text.Encoding.GetEncoding("GB2312") 'set the proper character set here
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:ddetering
Comment Utility
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.
0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
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.

<configuration>
   <system.web>
      <globalization
           fileEncoding="utf-8"
           requestEncoding="utf-8"
           responseEncoding="utf-8"
           culture="en-US"
           uiCulture="de-DE"
        />
   </system.web>
</configuration>

          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.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskeditingwebconfigfileforwebformsglobalization.asp

Thats really the only other thing I can think of

Aeros
0
 
LVL 1

Author Comment

by:ddetering
Comment Utility
I can't get it work with the system.web.mail class. Just have sent an inquiry to aspnetmail whether their product does it.
0
 
LVL 1

Author Comment

by:ddetering
Comment Utility
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now