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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48
