获取邮件中的HTML页面
下面的函数假定已经将邮件的几个部分分开
1private string GetText_Html(string PartSrc)
2{
3//获取传输编码
4Regex TransferEncoding_Regex =
5new Regex(@"Content-Transfer-Encoding: (.*?)\r\n", RegexOptions.IgnoreCase);
6Match MacthTransfer = TransferEncoding_Regex.Match(PartSrc);
7//截取出的内容文本
8string ContentStr = "";
9//用正则表达式截取内容部分
10//正则表达式声明中一定写上不区分大小写
11Regex HtmlContent_Regex =
12new Regex(@"<html([\s\S]*?)</html>", RegexOptions.IgnoreCase);
13Match MatchHtml = HtmlContent_Regex.Match(PartSrc);
14if (MatchHtml != null)
15{
16ContentStr = MatchHtml.Groups[1].Value;
17}
18//去除首尾的\r\n
19ContentStr = ContentStr.TrimStart(new char[] { '\r', '\n' });
20ContentStr = ContentStr.TrimEnd(new char[] { '\r', '\n' });
21
22//根据传输编码处理数据
23if (MacthTransfer.Groups[1].Value == "base64")
24{
25return Encoding.GetEncoding("GB2312").GetString(Convert.FromBase64String(ContentStr));
26}
27else//暂时将base64外的文本传输编码默认为打印机编码 quoted-printable
28{
29ArrayList CBuffer = new ArrayList();
30for (int i = 0; i < ContentStr.Length; i++)
31{
32if (ContentStr[i] == '=')
33{
34i++;
35if (ContentStr[i] != '\r')
36{
37byte vByte;
38if (byte.TryParse(ContentStr.Substring(i, 2),
39NumberStyles.HexNumber, null, out vByte))
40CBuffer.Add(vByte);
41}
42i++;
43}
44else if (ContentStr[i] != '\n') CBuffer.Add((byte)ContentStr[i]);
45}
46return Encoding.Default.GetString((byte[])CBuffer.ToArray(typeof(byte)));
47}
48}
2{
3//获取传输编码
4Regex TransferEncoding_Regex =
5new Regex(@"Content-Transfer-Encoding: (.*?)\r\n", RegexOptions.IgnoreCase);
6Match MacthTransfer = TransferEncoding_Regex.Match(PartSrc);
7//截取出的内容文本
8string ContentStr = "";
9//用正则表达式截取内容部分
10//正则表达式声明中一定写上不区分大小写
11Regex HtmlContent_Regex =
12new Regex(@"<html([\s\S]*?)</html>", RegexOptions.IgnoreCase);
13Match MatchHtml = HtmlContent_Regex.Match(PartSrc);
14if (MatchHtml != null)
15{
16ContentStr = MatchHtml.Groups[1].Value;
17}
18//去除首尾的\r\n
19ContentStr = ContentStr.TrimStart(new char[] { '\r', '\n' });
20ContentStr = ContentStr.TrimEnd(new char[] { '\r', '\n' });
21
22//根据传输编码处理数据
23if (MacthTransfer.Groups[1].Value == "base64")
24{
25return Encoding.GetEncoding("GB2312").GetString(Convert.FromBase64String(ContentStr));
26}
27else//暂时将base64外的文本传输编码默认为打印机编码 quoted-printable
28{
29ArrayList CBuffer = new ArrayList();
30for (int i = 0; i < ContentStr.Length; i++)
31{
32if (ContentStr[i] == '=')
33{
34i++;
35if (ContentStr[i] != '\r')
36{
37byte vByte;
38if (byte.TryParse(ContentStr.Substring(i, 2),
39NumberStyles.HexNumber, null, out vByte))
40CBuffer.Add(vByte);
41}
42i++;
43}
44else if (ContentStr[i] != '\n') CBuffer.Add((byte)ContentStr[i]);
45}
46return Encoding.Default.GetString((byte[])CBuffer.ToArray(typeof(byte)));
47}
48}