S
Shan Plourde
Hi there,
I have an e-commerce website that sends automated emails that contain
an automatically generated PDF attachment. It's similar to this email
sample:
------
Email Sample:
Subject: <Name> just completed their assessment
Attachment: <Name> assessment results.PDF
Body: This is a friendly alert that <Name> just completed their
assessment...
------
The system correctly stores <Name> in a SQL Server nvarchar database
column. Some names stored use Latin characters, while others use
Chinese characters. Notably the issue that I've just recently found is
that the automatically generated emails are not showing <Name>
correctly if the person's name happens to have Chinese characters in
it.
Here is the code that is used to create and send the emails:
------
SmtpClient client = new SmtpClient();
MailAddress from = new MailAddress(this.emailFrom);
MailAddress to = new MailAddress(this.emailTo,
String.Format("{0} {1}", this.firstName,
this.lastName));
MailMessage msg = new MailMessage(from, to);
if (this.attachment != null)
{
this.attachment.NameEncoding =
System.Text.Encoding.UTF8;
msg.Attachments.Add(this.attachment);
}
msg.SubjectEncoding = System.Text.Encoding.UTF8;
// The line below sets the subject to something like:
// "<Name> just completed their assessment"
msg.Subject =
templateManager.ProcessFileTemplate("AssessmentComplete.Subject.vm",
context);
msg.Body = templateManager.ProcessFileTemplate(
"AssessmentComplete.vm", context);
msg.BodyEncoding = System.Text.Encoding.UTF8;
client.Send(msg);
------
I built this awhile back assuming that since I was setting all email
encodings to UTF8, the email system would work for various languages.
Unfortunately that's not the case. When an automatically generated
email is sent where the person's name contains Chinese characters, the
subject will typically read:
"??? ?? ??? just completed their assessment"
The email body however correctly displays the Chinese name. The PDF
file attachment name will be named:
"??? ?? ??? assessment results.PDF"
msg.Subject does write to console and shows in the debugger correctly
with the Chinese characters, as does the body and file attachment
name. So the storage of the Chinese characters is correct, as is the
retrieval into .NET String objects.
The error seems to be happening during the transport of the email I
suspect. When I hard-code the subject and attachment encodings to
something such as System.Text.Encoding.GetEncoding("gb2312"), which is
Simplified Chinese, the Chinese characters display correctly.
Why wouldn't UTF8 encoding work though? Also, I'm not able to simply
hardcode an encoding of "gb2312" as the subject and file attachment
encoding - what happens if names are stored in other languages? This
would clearly fail. Maybe there's a way to guess at the encoding of
a .NET string, but I'm not aware of one. Shouldn't UTF8 work in the
first place?
What do people normally do to handle this? Should I create email
subject lines with Unicode escape codes for all characters? If so, is
there an out of the box approach to do this?
Confused!
Thanks for your help,
Shan
I have an e-commerce website that sends automated emails that contain
an automatically generated PDF attachment. It's similar to this email
sample:
------
Email Sample:
Subject: <Name> just completed their assessment
Attachment: <Name> assessment results.PDF
Body: This is a friendly alert that <Name> just completed their
assessment...
------
The system correctly stores <Name> in a SQL Server nvarchar database
column. Some names stored use Latin characters, while others use
Chinese characters. Notably the issue that I've just recently found is
that the automatically generated emails are not showing <Name>
correctly if the person's name happens to have Chinese characters in
it.
Here is the code that is used to create and send the emails:
------
SmtpClient client = new SmtpClient();
MailAddress from = new MailAddress(this.emailFrom);
MailAddress to = new MailAddress(this.emailTo,
String.Format("{0} {1}", this.firstName,
this.lastName));
MailMessage msg = new MailMessage(from, to);
if (this.attachment != null)
{
this.attachment.NameEncoding =
System.Text.Encoding.UTF8;
msg.Attachments.Add(this.attachment);
}
msg.SubjectEncoding = System.Text.Encoding.UTF8;
// The line below sets the subject to something like:
// "<Name> just completed their assessment"
msg.Subject =
templateManager.ProcessFileTemplate("AssessmentComplete.Subject.vm",
context);
msg.Body = templateManager.ProcessFileTemplate(
"AssessmentComplete.vm", context);
msg.BodyEncoding = System.Text.Encoding.UTF8;
client.Send(msg);
------
I built this awhile back assuming that since I was setting all email
encodings to UTF8, the email system would work for various languages.
Unfortunately that's not the case. When an automatically generated
email is sent where the person's name contains Chinese characters, the
subject will typically read:
"??? ?? ??? just completed their assessment"
The email body however correctly displays the Chinese name. The PDF
file attachment name will be named:
"??? ?? ??? assessment results.PDF"
msg.Subject does write to console and shows in the debugger correctly
with the Chinese characters, as does the body and file attachment
name. So the storage of the Chinese characters is correct, as is the
retrieval into .NET String objects.
The error seems to be happening during the transport of the email I
suspect. When I hard-code the subject and attachment encodings to
something such as System.Text.Encoding.GetEncoding("gb2312"), which is
Simplified Chinese, the Chinese characters display correctly.
Why wouldn't UTF8 encoding work though? Also, I'm not able to simply
hardcode an encoding of "gb2312" as the subject and file attachment
encoding - what happens if names are stored in other languages? This
would clearly fail. Maybe there's a way to guess at the encoding of
a .NET string, but I'm not aware of one. Shouldn't UTF8 work in the
first place?
What do people normally do to handle this? Should I create email
subject lines with Unicode escape codes for all characters? If so, is
there an out of the box approach to do this?
Confused!
Thanks for your help,
Shan