分开邮件的各个部分,应该是用正则了.分开之后,对内容做读取的时候,需要用base64和qp两种解码,要根据邮件相关信息来确定具体用哪一种.

一般邮件获取内容编码方式的正则的写法应该是下面这个样子

C# code
private const string encodingReg = "(?<=(Content\\-Transfer\\-Encoding\\:)).*";


做一个TextAnalyze方法来统筹分析

C# code
//c 内容 //charset,文字编码方式,utf-8/gb2312等等 //encoding,加密编码方式,包括qp和base64两种 //因为encoding获得的加密编码标志可能不同,所以用了switch. private string TextAnalyze(string c, string charset, string encoding) { switch (encoding.ToLower()) { case "quoted-printable": case "qp": case "q": try { c = QDecode(Encoding.GetEncoding(charset), c); } catch (Exception e) { throw new Exception("TextAnalyze(quoted-printabl)" + e.Message); } break; case "base64": case "b": try { c = DecodeBase64(charset, encoding); } catch (Exception e) { throw new Exception("TextAnalyze(DecodeBase64)" + e.Message); } break; } return c; }


base64在c#中处理很简单了,就放一个qp的解码.这个也是在网上辛苦搜到的.当时自己水平太凹,总编不好.

C# code
/// <summary> /// quoted-printable解码程序. /// </summary> /// <param name="encoding">解码目标字符集</param> /// <param name="data">需要解码的字符串</param> /// <returns></returns> private static string QDecode(System.Text.Encoding encoding, string data) { 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()); }


当时对我来讲最困难的就是qp这部分了,这部分解决之后其他的都很好办的.不知道你什么情况.

posted on 2010-07-29 15:26  freedom831215  阅读(829)  评论(0编辑  收藏  举报