此方法将文字编码为QuotedPrintable格式
public static string EncodeQuotedPrintable(string value)
{
if (string.IsNullOrEmpty(value))
return value;
StringBuilder builder = new StringBuilder();
byte[] bytes = Encoding.UTF8.GetBytes(value);
foreach (byte v in bytes)
{
// The following are not required to be encoded:
// - Tab (ASCII 9)
// - Space (ASCII 32)
// - Characters 33 to 126, except for the equal sign (61).
if ((v == 9) || ((v >= 32) && (v <= 60)) || ((v >= 62) && (v <= 126)))
{
builder.Append(Convert.ToChar(v));
}
else
{
builder.Append('=');
builder.Append(v.ToString("X2"));
}
}
char lastChar = builder[builder.Length - 1];
if (char.IsWhiteSpace(lastChar))
{
builder.Remove(builder.Length - 1, 1);
builder.Append('=');
builder.Append(((int)lastChar).ToString("X2"));
}
return builder.ToString();
}
此方法将QuotedPrintable格式的文本解码为字符串
public static string DecodeQuotedPrintable(string input, string charSet)
{
Encoding enc;
try
{
enc = Encoding.GetEncoding(charSet);
}
catch
{
enc = new UTF8Encoding();
}
var occurences = new Regex(@"(=[0-9A-Z]{2}){1,}", RegexOptions.Multiline);
var matches = occurences.Matches(input);
foreach (Match match in matches)
{
try
{
byte[] b = new byte[match.Groups[0].Value.Length / 3];
for (int i = 0; i < match.Groups[0].Value.Length / 3; i++)
{
b[i] = byte.Parse(match.Groups[0].Value.Substring(i * 3 + 1, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
}
char[] hexChar = enc.GetChars(b);
input = input.Replace(match.Groups[0].Value, new String(hexChar));
}
catch
{ ;}
}
input = input.Replace("?=", "").Replace("\r\n", "");
return input;
}