如何解码在 C# 中编码的引用可打印的电子邮件
How to decode the email quoted printable encoded in C#
我在这里寻找了很多解决方案,但仍然没有找到解码电子邮件引用可打印的工作解决方案。
示例输入:
*** Hello, World *** =0D=0AURl: http://w=
ww.example.com?id=3D=
27a9dca9-5d61-477c-8e73-a76666b5b1bf=0D=0A=0D=0A
Name: Hello World=0D=0A
Phone: 61234567890=0D=0A
Email: hello@test.com=0D=0A=0D=0A
示例预期输出为:
*** Hello, World ***
URl: http://www.example.com?id=27a9dca9-5d61-477c-8e73-a76666b5b1bf
Name: Hello World
Phone: 61234567890
Email: hello@test.com
以下www.webatic.com/quoted-printable-convertor是正确的渲染。
有人知道用 C# 解决这个问题吗?
尝试使用下面的代码片段来解码 Quoted Printable 编码
class Program
{
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("?=", "");
return input;
}
static void Main(string[] args)
{
string sData = @"*** Hello, World *** =0D=0AURl: http://www.example.com?id=3D=27a9dca9-5d61-477c-8e73-a76666b5b1bf=0D=0A=0D=0A
Name: Hello World=0D=0A
Phone: 61234567890=0D=0A
Email: hello@test.com=0D=0A=0D=0A";
Console.WriteLine(DecodeQuotedPrintable(sData,"utf-8"));
Console.ReadLine();
}
}
运行 代码在 dotnetfiddle
中可用
摘自 this link
的片段
我在这里寻找了很多解决方案,但仍然没有找到解码电子邮件引用可打印的工作解决方案。
示例输入:
*** Hello, World *** =0D=0AURl: http://w=
ww.example.com?id=3D=
27a9dca9-5d61-477c-8e73-a76666b5b1bf=0D=0A=0D=0A
Name: Hello World=0D=0A
Phone: 61234567890=0D=0A
Email: hello@test.com=0D=0A=0D=0A
示例预期输出为:
*** Hello, World ***
URl: http://www.example.com?id=27a9dca9-5d61-477c-8e73-a76666b5b1bf
Name: Hello World
Phone: 61234567890
Email: hello@test.com
以下www.webatic.com/quoted-printable-convertor是正确的渲染。
有人知道用 C# 解决这个问题吗?
尝试使用下面的代码片段来解码 Quoted Printable 编码
class Program
{
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("?=", "");
return input;
}
static void Main(string[] args)
{
string sData = @"*** Hello, World *** =0D=0AURl: http://www.example.com?id=3D=27a9dca9-5d61-477c-8e73-a76666b5b1bf=0D=0A=0D=0A
Name: Hello World=0D=0A
Phone: 61234567890=0D=0A
Email: hello@test.com=0D=0A=0D=0A";
Console.WriteLine(DecodeQuotedPrintable(sData,"utf-8"));
Console.ReadLine();
}
}
运行 代码在 dotnetfiddle
中可用摘自 this link
的片段