Send email with Korean characters in subject line and body of email

  • Thread starter Thread starter Kevin B Ebert
  • Start date Start date
K

Kevin B Ebert

For the life of me, I can't figure this out. I'm trying to send out
an email written in Korean using SMTP. When I send the message to an
Outlook client, everything works like a champ. But, when I send the
email to a web client, all the characters get screwed up and the email
is not legible. I figured the
System.Web.Mail.MailMessage.BodyEncoding property would control the
encoding of the characters, but, it doesn't seem to help. I've
included some code below:

System.Web.Mail.MailMessage message = new
System.Web.Mail.MailMessage();
// As far as I understand, Code Page 949 is for Korean Characters.
// I've also tried System.Text.Encoding.UTF8 to no avail.
message.BodyEncoding = System.Text.Encoding.GetEncoding(949);
message.BodyFormat = System.Web.Mail.MailFormat.Html;
// The body of the message is pulled from our SQL database.
// When comes in, the Korean characters look fine.
message.Body = "This is where my korean character formatted email will
go.";
message.Cc = "(e-mail address removed)";
message.From = "(e-mail address removed)";
message.Subject = "Subject line";
message.To = "(e-mail address removed)";

I can tell you, I've configured my browser to allow for Korean
characters. So, I don't suspect it is a browser problem. And, to add
complexity, when I compose my email message in Outlook and send it to
my web mail, the message is fine, it works like a champ. So,
therefore, I'm assuming I just don't understand something about
encoding strings. I also tried taking my string and converting to
UTF8 formatted string hoping that would help. I changed the code
above to the following:

System.Web.Mail.MailMessage message = new
System.Web.Mail.MailMessage();
// Need to convert the body string to UTF8
// The body of the message is pulled from our SQL database.
// When comes in, the Korean characters look fine.
string unicodeString = "This is where my korean character formatted
email will go.";
// Create two different encodings.
System.Text.Encoding utf8 = System.Text.Encoding.UTF8
System.Text.Encoding unicode = System.Text.Encoding.Unicode;
// Convert the string into a byte[].
byte[] unicodeBytes = unicode.GetBytes(unicodeString);
// Perform the conversion from one encoding to the other.
byte[] utf8Bytes = System.Text.Encoding.Convert(unicode, utf8,
unicodeBytes);
// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char[] utf8Chars = new char[utf8.GetCharCount(utf8Bytes, 0,
utf8Bytes.Length)];
utf8.GetChars(utf8Bytes, 0, utf8Bytes.Length, utf8Chars, 0);
string utf8String = new string(utf8Chars);

message.Body = utf8String;
// Build up the rest of the message
message.Cc = "(e-mail address removed)";
message.From = "(e-mail address removed)";
message.Subject = "Subject line";
message.To = "(e-mail address removed)";

But, this doesn't help either. Below is a small sampling of what
happens to the characters when I view them in my email client.

KTF ±ÂŸÀÓ ´Ü¸»±â º¸Çè¿¡ °¡ÀÔÇØ Áּż­ °¨»çµå¸³´Ï´Ù. º¸Çè ó¸®
´ÙÀ½´Ü°è·Î ³Ñ¾î°¡±â À§ÇÑ ÀýÂ÷´Â ´ÙÀ½°ú °°½À´Ï´Ù.
 
All strings (System.String) in the .NET Framework are Unicode (as are
Chars). Converting to bytes doesn't do anything if you convert right back to
a string.

The Hangeul you posted at the end of the message shows up just fine in
Outlook Express, after I set the encoding to Korean.

-mike
MVP

Kevin B Ebert said:
For the life of me, I can't figure this out. I'm trying to send out
an email written in Korean using SMTP. When I send the message to an
Outlook client, everything works like a champ. But, when I send the
email to a web client, all the characters get screwed up and the email
is not legible. I figured the
System.Web.Mail.MailMessage.BodyEncoding property would control the
encoding of the characters, but, it doesn't seem to help. I've
included some code below:

System.Web.Mail.MailMessage message = new
System.Web.Mail.MailMessage();
// As far as I understand, Code Page 949 is for Korean Characters.
// I've also tried System.Text.Encoding.UTF8 to no avail.
message.BodyEncoding = System.Text.Encoding.GetEncoding(949);
message.BodyFormat = System.Web.Mail.MailFormat.Html;
// The body of the message is pulled from our SQL database.
// When comes in, the Korean characters look fine.
message.Body = "This is where my korean character formatted email will
go.";
message.Cc = "(e-mail address removed)";
message.From = "(e-mail address removed)";
message.Subject = "Subject line";
message.To = "(e-mail address removed)";

I can tell you, I've configured my browser to allow for Korean
characters. So, I don't suspect it is a browser problem. And, to add
complexity, when I compose my email message in Outlook and send it to
my web mail, the message is fine, it works like a champ. So,
therefore, I'm assuming I just don't understand something about
encoding strings. I also tried taking my string and converting to
UTF8 formatted string hoping that would help. I changed the code
above to the following:

System.Web.Mail.MailMessage message = new
System.Web.Mail.MailMessage();
// Need to convert the body string to UTF8
// The body of the message is pulled from our SQL database.
// When comes in, the Korean characters look fine.
string unicodeString = "This is where my korean character formatted
email will go.";
// Create two different encodings.
System.Text.Encoding utf8 = System.Text.Encoding.UTF8
System.Text.Encoding unicode = System.Text.Encoding.Unicode;
// Convert the string into a byte[].
byte[] unicodeBytes = unicode.GetBytes(unicodeString);
// Perform the conversion from one encoding to the other.
byte[] utf8Bytes = System.Text.Encoding.Convert(unicode, utf8,
unicodeBytes);
// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char[] utf8Chars = new char[utf8.GetCharCount(utf8Bytes, 0,
utf8Bytes.Length)];
utf8.GetChars(utf8Bytes, 0, utf8Bytes.Length, utf8Chars, 0);
string utf8String = new string(utf8Chars);

message.Body = utf8String;
// Build up the rest of the message
message.Cc = "(e-mail address removed)";
message.From = "(e-mail address removed)";
message.Subject = "Subject line";
message.To = "(e-mail address removed)";

But, this doesn't help either. Below is a small sampling of what
happens to the characters when I view them in my email client.

KTF 굿타임 단ë§ê¸° ë³´í—˜ì— ê°€ìž…í•´ 주셔서 ê°ì‚¬ë“œë¦½ë‹ˆë‹¤. ë³´í—˜ 처리
다ìŒë‹¨ê³„ë¡œ 넘어가기 위한 절차는 다ìŒê³¼ 같습니다.
 
Thanks for the reply. It turns out the problem is inside the encoding
of the HTML page that displays the email message. So, to get around
this, I ended up adding the header: Content-Type: text/html;
charset="UTF-8" to the email message and also created a META tag
specifying an encoding of UTF-8 to the message of the email. I
derived this solution by inspecting the email that I sent from my
Outlook client. It places this META tag on the email message
automatically.

So, the good news the solution works some of the time. The bad news,
it doesn't work some of the time. My browser is set to automatically
detect the page encoding. So, when I view the email in my Yahoo!
account, it picks up the META tag I added and sets the encoding to
UTF-8. But, with Hotmail, they apparently strip the META tag out and
therefore, if my browser is set to automatically detect the encoding,
it doesn't pick up on the appropriate encoding and therefore, doesn't
display the message correctly. So, I'm stuck with what to do next. I
have some folks in Korea testing the solution, but it will only work
in Hotmail if their browser encoding is set to UTF-8. I just don't
know how realistic it is to expect people to have their browser
configured this way.

A follow up question is, what good does the following line of code do:

message.BodyEncoding = System.Text.Encoding.UTF8;

By itself, I couldn't see any benefit in the email message. It didn't
add a header to the email nor did it add the META tag to the body of
the email. Could this be a bug in the .Net Framework? I would
suspect that specifying the BodyEncoding of the
System.Mail.MailMessage object should add the appropriate header to
the email, but to the best of my knowledge, I couldn't see this.


Michael Giagnocavo said:
All strings (System.String) in the .NET Framework are Unicode (as are
Chars). Converting to bytes doesn't do anything if you convert right back to
a string.

The Hangeul you posted at the end of the message shows up just fine in
Outlook Express, after I set the encoding to Korean.

-mike
MVP

Kevin B Ebert said:
For the life of me, I can't figure this out. I'm trying to send out
an email written in Korean using SMTP. When I send the message to an
Outlook client, everything works like a champ. But, when I send the
email to a web client, all the characters get screwed up and the email
is not legible. I figured the
System.Web.Mail.MailMessage.BodyEncoding property would control the
encoding of the characters, but, it doesn't seem to help. I've
included some code below:

System.Web.Mail.MailMessage message = new
System.Web.Mail.MailMessage();
// As far as I understand, Code Page 949 is for Korean Characters.
// I've also tried System.Text.Encoding.UTF8 to no avail.
message.BodyEncoding = System.Text.Encoding.GetEncoding(949);
message.BodyFormat = System.Web.Mail.MailFormat.Html;
// The body of the message is pulled from our SQL database.
// When comes in, the Korean characters look fine.
message.Body = "This is where my korean character formatted email will
go.";
message.Cc = "(e-mail address removed)";
message.From = "(e-mail address removed)";
message.Subject = "Subject line";
message.To = "(e-mail address removed)";

I can tell you, I've configured my browser to allow for Korean
characters. So, I don't suspect it is a browser problem. And, to add
complexity, when I compose my email message in Outlook and send it to
my web mail, the message is fine, it works like a champ. So,
therefore, I'm assuming I just don't understand something about
encoding strings. I also tried taking my string and converting to
UTF8 formatted string hoping that would help. I changed the code
above to the following:

System.Web.Mail.MailMessage message = new
System.Web.Mail.MailMessage();
// Need to convert the body string to UTF8
// The body of the message is pulled from our SQL database.
// When comes in, the Korean characters look fine.
string unicodeString = "This is where my korean character formatted
email will go.";
// Create two different encodings.
System.Text.Encoding utf8 = System.Text.Encoding.UTF8
System.Text.Encoding unicode = System.Text.Encoding.Unicode;
// Convert the string into a byte[].
byte[] unicodeBytes = unicode.GetBytes(unicodeString);
// Perform the conversion from one encoding to the other.
byte[] utf8Bytes = System.Text.Encoding.Convert(unicode, utf8,
unicodeBytes);
// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char[] utf8Chars = new char[utf8.GetCharCount(utf8Bytes, 0,
utf8Bytes.Length)];
utf8.GetChars(utf8Bytes, 0, utf8Bytes.Length, utf8Chars, 0);
string utf8String = new string(utf8Chars);

message.Body = utf8String;
// Build up the rest of the message
message.Cc = "(e-mail address removed)";
message.From = "(e-mail address removed)";
message.Subject = "Subject line";
message.To = "(e-mail address removed)";

But, this doesn't help either. Below is a small sampling of what
happens to the characters when I view them in my email client.

KTF 굿타임 단ë§ê¸° ë³´í—˜ì— ê°€ìž…í•´ 주셔서 ê°ì‚¬ë“œë¦½ë‹ˆë‹¤. ë³´í—˜ 처리
다ìŒë‹¨ê³„ë¡œ 넘어가기 위한 절차는 다ìŒê³¼ 같습니다.
 
I don't know more detailed answers to your problem. I do know that IE tries
to "auto detect" based on certain metrics. Depending on the content of the
page, IE will detect a different encoding. They did this as a nice feature,
and it works most of the time, like you pointed out.

What happens if you use the Korean default charset? I'd guess that using
Unicode would be quite common on newer machines, but the best way would be
to survey your target audience and see what configurations are setup as
default.

-Michael
MVP

Kevin B Ebert said:
Thanks for the reply. It turns out the problem is inside the encoding
of the HTML page that displays the email message. So, to get around
this, I ended up adding the header: Content-Type: text/html;
charset="UTF-8" to the email message and also created a META tag
specifying an encoding of UTF-8 to the message of the email. I
derived this solution by inspecting the email that I sent from my
Outlook client. It places this META tag on the email message
automatically.

So, the good news the solution works some of the time. The bad news,
it doesn't work some of the time. My browser is set to automatically
detect the page encoding. So, when I view the email in my Yahoo!
account, it picks up the META tag I added and sets the encoding to
UTF-8. But, with Hotmail, they apparently strip the META tag out and
therefore, if my browser is set to automatically detect the encoding,
it doesn't pick up on the appropriate encoding and therefore, doesn't
display the message correctly. So, I'm stuck with what to do next. I
have some folks in Korea testing the solution, but it will only work
in Hotmail if their browser encoding is set to UTF-8. I just don't
know how realistic it is to expect people to have their browser
configured this way.

A follow up question is, what good does the following line of code do:

message.BodyEncoding = System.Text.Encoding.UTF8;

By itself, I couldn't see any benefit in the email message. It didn't
add a header to the email nor did it add the META tag to the body of
the email. Could this be a bug in the .Net Framework? I would
suspect that specifying the BodyEncoding of the
System.Mail.MailMessage object should add the appropriate header to
the email, but to the best of my knowledge, I couldn't see this.


Michael Giagnocavo said:
All strings (System.String) in the .NET Framework are Unicode (as are
Chars). Converting to bytes doesn't do anything if you convert right back
to
a string.

The Hangeul you posted at the end of the message shows up just fine in
Outlook Express, after I set the encoding to Korean.

-mike
MVP

Kevin B Ebert said:
For the life of me, I can't figure this out. I'm trying to send out
an email written in Korean using SMTP. When I send the message to an
Outlook client, everything works like a champ. But, when I send the
email to a web client, all the characters get screwed up and the email
is not legible. I figured the
System.Web.Mail.MailMessage.BodyEncoding property would control the
encoding of the characters, but, it doesn't seem to help. I've
included some code below:

System.Web.Mail.MailMessage message = new
System.Web.Mail.MailMessage();
// As far as I understand, Code Page 949 is for Korean Characters.
// I've also tried System.Text.Encoding.UTF8 to no avail.
message.BodyEncoding = System.Text.Encoding.GetEncoding(949);
message.BodyFormat = System.Web.Mail.MailFormat.Html;
// The body of the message is pulled from our SQL database.
// When comes in, the Korean characters look fine.
message.Body = "This is where my korean character formatted email will
go.";
message.Cc = "(e-mail address removed)";
message.From = "(e-mail address removed)";
message.Subject = "Subject line";
message.To = "(e-mail address removed)";

I can tell you, I've configured my browser to allow for Korean
characters. So, I don't suspect it is a browser problem. And, to add
complexity, when I compose my email message in Outlook and send it to
my web mail, the message is fine, it works like a champ. So,
therefore, I'm assuming I just don't understand something about
encoding strings. I also tried taking my string and converting to
UTF8 formatted string hoping that would help. I changed the code
above to the following:

System.Web.Mail.MailMessage message = new
System.Web.Mail.MailMessage();
// Need to convert the body string to UTF8
// The body of the message is pulled from our SQL database.
// When comes in, the Korean characters look fine.
string unicodeString = "This is where my korean character formatted
email will go.";
// Create two different encodings.
System.Text.Encoding utf8 = System.Text.Encoding.UTF8
System.Text.Encoding unicode = System.Text.Encoding.Unicode;
// Convert the string into a byte[].
byte[] unicodeBytes = unicode.GetBytes(unicodeString);
// Perform the conversion from one encoding to the other.
byte[] utf8Bytes = System.Text.Encoding.Convert(unicode, utf8,
unicodeBytes);
// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char[] utf8Chars = new char[utf8.GetCharCount(utf8Bytes, 0,
utf8Bytes.Length)];
utf8.GetChars(utf8Bytes, 0, utf8Bytes.Length, utf8Chars, 0);
string utf8String = new string(utf8Chars);

message.Body = utf8String;
// Build up the rest of the message
message.Cc = "(e-mail address removed)";
message.From = "(e-mail address removed)";
message.Subject = "Subject line";
message.To = "(e-mail address removed)";

But, this doesn't help either. Below is a small sampling of what
happens to the characters when I view them in my email client.

KTF 굿타임 단ë§ê¸° ë³´í—˜ì— ê°€ìž…í•´ 주셔서 ê°ì‚¬ë“œë¦½ë‹ˆë‹¤. ë³´í—˜ 처리
다ìŒë‹¨ê³„ë¡œ 넘어가기 위한 절차는 다ìŒê³¼ 같습니다.
 
Back
Top