Esempio n. 1
0
        public static string Decode(string literals, string transferEncoding, string charset)
        {
            Encoding targetEncoding;

            // Cp1252 is not recognized under this name
            if (charset.ToLower() == "cp1252")
            {
                charset = Charsets.Ansi;
            }

            try {
                // if this goes haywire
                targetEncoding = Encoding.GetEncoding(charset);
            }
            catch (Exception) {
                // try this one
                targetEncoding = Encoding.UTF8;
            }

            switch (transferEncoding.ToLower())
            {
            case ContentTransferEncodings.QuotedPrintable:
            {
                return(QuotedPrintableConverter.FromQuotedPrintable(literals, targetEncoding));
            }

            case ContentTransferEncodings.Base64:
            {
                var bytes = Convert.FromBase64String(literals);
                return(targetEncoding.GetString(bytes));
            }

            default:
            {
                // no encoding
                return(literals);
            }
            }
        }
Esempio n. 2
0
        public static string Encode(string text, string transferEncoding, string charset, bool useBockText = true)
        {
            switch (transferEncoding)
            {
            case ContentTransferEncodings.QuotedPrintable:
            {
                var bytes = Encoding.UTF8.GetBytes(text);
                return(QuotedPrintableConverter.ToQuotedPrintableString(bytes));
            }

            case ContentTransferEncodings.None:
            {
                return(text);
            }

            default:
            {
                var bytes       = Encoding.UTF8.GetBytes(text);
                var encodedText = Convert.ToBase64String(bytes);
                return(useBockText ? encodedText.ToBlockText(76) : encodedText);
            }
            }
        }