japanese email subject encoding

后端 未结 9 1556
天命终不由人
天命终不由人 2020-12-15 08:02

Aparently, encoding japanese emails is somewhat challenging, which I am slowly discovering myself. In case there are any experts (even those with limited experience will do)

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-15 08:45

    I have some experience composing and sending email in japanese...Normally you have to beware what encoding used for operating system and how you store your japanese strings! My Mail objects are normally encoded as follows:

        string s = "V‚µ‚¢ŠwK–@‚Ì‚²’ñˆÄ"; // Our japanese are shift-jis encoded, so it appears like garbled
        MailMessage message = new MailMessage();
        message.BodyEncoding = Encoding.GetEncoding("iso-2022-jp");
        message.SubjectEncoding = Encoding.GetEncoding("iso-2022-jp");
        message.Subject = s.ToEncoding(Encoding.GetEncoding("Shift-Jis")); // Change the encoding to whatever your source is
        message.Body = s.ToEncoding(Encoding.GetEncoding("Shift-Jis")); // Change the encoding to whatever your source is
    

    Then i have an extension method to which does the conversion for me:

    public static string ToEncoding(this string s, Encoding targetEncoding)
            {   
                return s == null ? null : targetEncoding.GetString(Encoding.GetEncoding(1252).GetBytes(s)); //1252 is the windows OS codepage            
            }
    

提交回复
热议问题