eml之quoted-printable 解码
2011-07-09 01:18
204 查看
这周在公司BOSS给了一个工作任务,其中要用POP来接收电子邮件,然后找出部分需要的东西,比如Url,处理的邮件都是Google Alert,在邮件中包含Base64和quoted-printable编码的内容,其中BASE64编码的部分都是纯文本的,不好处理,只好从quoted-printable编码的HTML中下手,但从网上找的quoted-printable解码方法很多时候都出错,原来换行的地方有三种可能 "=\r\n"、"=\r"、"=\n",以下为处理代码
借用了以下链接的代码
/// <summary> /// quoted-printable解码程序 /// </summary> /// <param name="encoding">解码目标字符集</param> /// <param name="data">需要解码的字符串</param> /// <returns></returns> private string QDecode(System.Text.Encoding encoding, string data) { data = data.Replace("=\r\n", "").Replace("=\n", "").Replace("=\r", ""); MemoryStream strm = new MemoryStream(System.Text.Encoding.Default.GetBytes(data)); int b = strm.ReadByte(); MemoryStream dStrm = new MemoryStream(); while (b > -1) { // Hex eg. =E4 if (b == '=') { byte[] buf = new byte[2]; strm.Read(buf, 0, 2); if (!(buf[0] == '\r' && buf[1] == '\n')) { int val = int.Parse(System.Text.Encoding.Default.GetString(buf), System.Globalization.NumberStyles.HexNumber); //int val = int.Parse(System.Text.Encoding.Default.GetString(buf)); byte[] temp = new Byte[] { (byte)val }; dStrm.Write(temp, 0, temp.Length); } } else { string encodedChar = encoding.GetString(new byte[] { (byte)b }); byte[] d = System.Text.Encoding.Default.GetBytes(encodedChar); dStrm.Write(d, 0, d.Length); } b = strm.ReadByte(); } return encoding.GetString(dStrm.ToArray()); }
借用了以下链接的代码
相关文章推荐
- 读邮件,1:解码quoted-printable
- C# Quoted-Printable编码、解码
- [C#] UTF-8 ENCODING=QUOTED-PRINTABLE 的解码和编码
- 用于Quoted-Printable编解码的JAVA类
- python模块之quopri: quoted-printable编解码
- Quoted-printable 编码介绍、编码解码转换
- 原创: Base64, Quoted-Printable 的解码与编码函数
- python模块之quopri: quoted-printable编解码
- C# 对Quoted-Printable进行解码的方法
- quoted-printable 编码与解码
- C# 对Quoted-Printable进行解码的方法
- Qt quoted Printable 解码
- Quoted-Printable解码后为何还是乱码
- MIME之Quoted-Printable编解码
- MIME之Quoted-Printable编解码 选择自 bhw98 的 Blog
- quoted-printable解码程序
- MIME之Quoted-Printable编解码
- Java中quoted-printable编码的实现
- Decodes a QuotedPrintable encoded string
- Quoted-printable和Base64 编码