示例#1
0
        public static EmailEncodingResult GetEmailEncoding(byte[] databytes)
        {
            var result = EncodingHelper.EmailEncoding.ScanCharSet(databytes);

            if (result != null)
            {
                return(result);
            }

            Ude.CharsetDetector detector = new Ude.CharsetDetector();
            detector.Feed(databytes, 0, databytes.Length);
            detector.DataEnd();


            if (!string.IsNullOrWhiteSpace(detector.Charset))
            {
                if (result == null)
                {
                    result = new EmailEncodingResult();
                }

                result.Charset = detector.Charset;
                return(result);
            }

            return(null);
        }
示例#2
0
        private string GetString(byte[] data, EmailEncodingResult encodingresult)
        {
            System.Text.Encoding encoding = null;
            if (encodingresult != null && !string.IsNullOrEmpty(encodingresult.Charset))
            {
                encoding = System.Text.Encoding.GetEncoding(encodingresult.Charset);
            }

            if (encoding == null)
            {
                encoding = System.Text.Encoding.UTF8;
            }

            string text = encoding.GetString(data);

            if (text != null && encodingresult != null && !string.IsNullOrWhiteSpace(encodingresult.CharSetText))
            {
                text = text.Replace(encodingresult.CharSetText, "charset=utf-8");
            }

            return(text);
        }