4

C# でメールを読むためのシステムを構築しています。件名の解析で問題が発生しました。これはエンコーディングに関連していると思われます。

私が読んでいる件名は次のとおりです: =?ISO-8859-1?Q?=E6=F8sd=E5f=F8sdf_sdfsdf?=、送信された元の件名はæøsdåføsdf sdfsdf(そこにノルウェー文字) です。

エンコーディングを変更したり、これを正しく解析したりする方法はありますか? これまでのところ、C# エンコーディング変換手法を使用して対象を utf8 にエンコードしようとしましたが、うまくいきませんでした。

これが私が試した解決策の1つです:

Encoding iso = Encoding.GetEncoding("iso-8859-1");
Encoding utf = Encoding.UTF8;
string decodedSubject =
    utf.GetString(Encoding.Convert(utf, iso,
                                   iso.GetBytes(m.Subject.Split('?')[3])));
4

2 に答える 2

7
    public static string DecodeEncodedWordValue(string mimeString)
    {
        var regex = new Regex(@"=\?(?<charset>.*?)\?(?<encoding>[qQbB])\?(?<value>.*?)\?=");
        var encodedString = mimeString;
        var decodedString = string.Empty;

        while (encodedString.Length > 0)
        {
            var match = regex.Match(encodedString);
            if (match.Success)
            {
                // If the match isn't at the start of the string, copy the initial few chars to the output
                decodedString += encodedString.Substring(0, match.Index);

                var charset = match.Groups["charset"].Value;
                var encoding = match.Groups["encoding"].Value.ToUpper();
                var value = match.Groups["value"].Value;

                if (encoding.Equals("B"))
                {
                    // Encoded value is Base-64
                    var bytes = Convert.FromBase64String(value);
                    decodedString += Encoding.GetEncoding(charset).GetString(bytes);
                }
                else if (encoding.Equals("Q"))
                {
                    // Encoded value is Quoted-Printable
                    // Parse looking for =XX where XX is hexadecimal
                    var regx = new Regex("(\\=([0-9A-F][0-9A-F]))", RegexOptions.IgnoreCase);
                    decodedString += regx.Replace(value, new MatchEvaluator(delegate(Match m)
                    {
                        var hex = m.Groups[2].Value;
                        var iHex = Convert.ToInt32(hex, 16);

                        // Return the string in the charset defined
                        var bytes = new byte[1];
                        bytes[0] = Convert.ToByte(iHex);
                        return Encoding.GetEncoding(charset).GetString(bytes);
                    }));
                    decodedString = decodedString.Replace('_', ' ');
                }
                else
                {
                    // Encoded value not known, return original string
                    // (Match should not be successful in this case, so this code may never get hit)
                    decodedString += encodedString;
                    break;
                }

                // Trim off up to and including the match, then we'll loop and try matching again.
                encodedString = encodedString.Substring(match.Index + match.Length);
            }
            else
            {
                // No match, not encoded, return original string
                decodedString += encodedString;
                break;
            }
        }
        return decodedString;
    }
于 2010-11-08T13:14:29.497 に答える
7

エンコーディングはquoted printableと呼ばれます。

この質問への回答を参照してください。

受け入れられた回答から適応:

public string DecodeQuotedPrintable(string value)
{
        Attachment attachment = Attachment.CreateAttachmentFromString("", value);
        return attachment.Name;
}

文字列を渡すと、=?ISO-8859-1?Q?=E6=F8sd=E5f=F8sdf_sdfsdf?="æøsdåføsdf_sdfsdf" が返されます。

于 2010-11-05T14:50:23.530 に答える