[Mime] QuotedPrintableEncoding帮助类 (转载)

点击下载 QuotedPrintableEncoding.rar

这个类是关于QuotedPrintableEncoding的帮助类
看下面代码吧

/// <summary>
/// 类说明:Assistant
/// 编 码 人:苏飞
/// 联系方式:361983679  
/// 更新网站:[url=http://www.cckan.net/thread-655-1-1.html]http://www.cckan.net/thread-655-1-1.html[/url]
/// </summary>
using System;
using System.IO;
using System.Text.RegularExpressions;
 
namespace DotNet.Utilities
{
    /// <summary>
    /// This class is based on the QuotedPrintable class written by Bill Gearhart
    /// </summary>
    public static class QuotedPrintableEncoding
    {
        private const string Equal = "=";
 
        private const string HexPattern = "(\=([0-9A-F][0-9A-F]))";
 
        public static string Decode(string contents)
        {
            if (contents == null)
            {
                throw new ArgumentNullException("contents");
            }
 
            using (StringWriter writer = new StringWriter())
            {
                using (StringReader reader = new StringReader(contents))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        /*remove trailing line whitespace that may have
                         been added by a mail transfer agent per rule
                         #3 of the Quoted Printable section of RFC 1521.*/
                        line.TrimEnd();
 
                        if (line.EndsWith(Equal))
                        {
                            writer.Write(DecodeLine(line));
                        } //handle soft line breaks for lines that end with an "="
                        else
                        {
                            writer.WriteLine(DecodeLine(line));
                        }
                    }
                }
                writer.Flush();
 
                return writer.ToString();
            }
        }
 
        private static string DecodeLine(string line)
        {
            if (line == null)
            {
                throw new ArgumentNullException("line");
            }
 
            Regex hexRegex = new Regex(HexPattern, RegexOptions.IgnoreCase);
 
            return hexRegex.Replace(line, new MatchEvaluator(HexMatchEvaluator));
        }
 
        private static string HexMatchEvaluator(Match m)
        {
            int dec = Convert.ToInt32(m.Groups[2].Value, 16);
            char character = Convert.ToChar(dec);
            return character.ToString();
        }
    }
}
原文地址:https://www.cnblogs.com/lizeyan/p/3628720.html