Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Encoding of email subject lines - garbled international characters

Posted on 2004-09-28
7
1,527 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
ID: 12175885
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
ID: 12175973
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
ID: 12176188
Your referencing the ones in your web.config here?

System.Text.Encoding.GetEncoding("GB2312") 'set the proper character set here
0
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.

 
LVL 1

Author Comment

by:ddetering
ID: 12176406
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
ID: 12178813
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
ID: 12192871
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
ID: 12213731
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

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

838 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