.NET Framework Developer Center >
.NET Development Forums
>
.NET Base Class Library
>
sending a E-mail subject in chinese using system.web.mail
sending a E-mail subject in chinese using system.web.mail
- hi
i am using system.web.mail to send my mails. i used UTF-8 encoding for encode my message body.
while i am sending a mail with chinese characters the message body is displaying correctly. but the subject field is not coming in chinese instead its showing question mark.
so some one help me to send the subject in chinese.
MailMessage
objMessage = new MailMessage (); string strSMTPHost = (string)ConfigurationSettings.AppSettings["SMTPServer"];objMessage.To = emailEntity.To;
objMessage.Cc = emailEntity.Cc;
objMessage.Bcc = emailEntity.Bcc;
objMessage.From = emailEntity.From;
objMessage.Subject = emailEntity.Subject ;
objMessage.Body = emailEntity.Body ;
objMessage.BodyFormat =
MailFormat.Html;objMessage.BodyEncoding = System.Text.
Encoding.UTF8;objMessage.Priority = emailEntity.EmailPriority;
SmtpMail
.SmtpServer = strSMTPHost; SmtpMail.Send(objMessage);
Answers
To send Unicode in email headers, you have to use Encoded Word (RFC1522).
My badly written code is (only the last line is required if you want to encode unconditionally):private String EncodedWord(string s) { bool MustEncode = false; foreach (Char c in s) if (Convert.ToInt32(c)>127) MustEncode=true; if (!MustEncode) return s; return "=?UTF-8?b?"+Convert.ToBase64String(new UTF8Encoding().GetBytes(s))+"?="; }
Hope this helps.
-kk
PS: You should be using System.Net.Mail instead of system.web.mail.- Edited byK.Kong Tuesday, June 10, 2008 3:37 PMForgot something
- Edited byK.Kong Wednesday, June 11, 2008 2:03 AMAdded PS
- Marked As Answer byBruno YuMSFT, ModeratorThursday, June 12, 2008 6:38 AM
All Replies
- Use the SubjectEncoding property.
Hans Passant.- Proposed As Answer byBruno YuMSFT, ModeratorThursday, June 12, 2008 6:38 AM
To send Unicode in email headers, you have to use Encoded Word (RFC1522).
My badly written code is (only the last line is required if you want to encode unconditionally):private String EncodedWord(string s) { bool MustEncode = false; foreach (Char c in s) if (Convert.ToInt32(c)>127) MustEncode=true; if (!MustEncode) return s; return "=?UTF-8?b?"+Convert.ToBase64String(new UTF8Encoding().GetBytes(s))+"?="; }
Hope this helps.
-kk
PS: You should be using System.Net.Mail instead of system.web.mail.- Edited byK.Kong Tuesday, June 10, 2008 3:37 PMForgot something
- Edited byK.Kong Wednesday, June 11, 2008 2:03 AMAdded PS
- Marked As Answer byBruno YuMSFT, ModeratorThursday, June 12, 2008 6:38 AM


