关于 c#:在 StreamReader 中搜索 base64 编码的内容?

 2022-01-08 

Search for base64 encoded content in StreamReader?

我需要从 IIS 的 SMTP 死信文件夹中解析 NDR 文件。附加到 NDR 的是经过 base64 编码的内容。这就是我需要达到的目标。我希望,使用 StreamReader,这将是一条线,但是当执行 .ReadLine 时,它??们是单独的线。

这里是一个 NDR 示例:

1
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
From: postmaster
To: hidden
Date: Thu, 12 Nov 2009 11:56:14 -0500
MIME-Version: 1.0
Content-Type: multipart/report; report-type=delivery-status;
    boundary="9B095B5ADSN=_01CA638BF4740C5500000045spinstitch.com"
X-DSNContext: 7ce717b1 - 1378 - 00000002 - C00402EF
Message-ID:
Subject: Delivery Status Notification (Failure)

This is a MIME-formatted message.  
Portions of this message may be unreadable without a MIME-capable mail program.

--9B095B5ADSN=_01CA638BF4740C5500000045spinstitch.com
Content-Type: text/plain; charset=unicode-1-1-utf-7

This is an automatically generated Delivery Status Notification.

Delivery to the following recipients failed.

       hidden




--9B095B5ADSN=_01CA638BF4740C5500000045spinstitch.com
Content-Type: message/delivery-status

Reporting-MTA: dns;spinstitch.com
Received-From-MTA: dns;spinstitch
Arrival-Date: Thu, 12 Nov 2009 11:56:11 -0500

Final-Recipient: rfc822;hidden
Action: failed
Status: 5.4.0

--9B095B5ADSN=_01CA638BF4740C5500000045spinstitch.com
Content-Type: message/rfc822

Received: from mail pickup service by spinstitch.com with Microsoft SMTPSVC;
     Thu, 12 Nov 2009 11:56:11 -0500
MIME-Version: 1.0
From: hidden
To: hidden
Date: 12 Nov 2009 11:56:11 -0500
Subject: For Sale: 463 Saltaire Dr. Calabash, NC US  28467 $6,800,000
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: base64
Message-ID:
X-OriginalArrivalTime: 12 Nov 2009 16:56:11.0813 (UTC) FILETIME=[09CED550:01CA63B9]

DQo8Y2VudGVyPg0KPGltZyBpZD0idHJJTUciIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHNyYz0i
aHR0cDovL3NlcnZpY2VzLnNwaW5zdGl0Y2guY29tL3RyaW1nLmFzcHg/Z2V0SW1nPUswaXg0
cDJkZ1c4b3ozRmZBNE5GcmJxVmFoUTZpOWd2Z2ZlZmVWcjhSZkVhamdnamFYUnROMWU2RW15
OG5ic0dUbWMxOEo0UnlhMS81WVFvaFhraFA2VTNFcTRFRHB4R1J4YmpjeXlhOG56aExKT2hD
M0QzL3RWNVh6ZmxWNkphIj4NCjxkaXYgc3R5bGU9IndpZHRoOjEwMCU7IGJhY2tncm91bmQ6
IHVybChodHRwOi8vd3d3LnNwaW5zdGl0Y2h0b3Vycy5jb21GaWxlcy9WaWV3ZXJTa2lucy9C
YWNrZ3JvdW5kcy9EZWZhdWx0L2JhY2suanBnKTsgYmFja2dyb3VuZC1hdHRhY2htZW50OmZp
eGVkOw0KCWJhY2tncm91bmQtcmVwZWF0OnJlcGVhdC15Ow0KCWJhY2tncm91bmQtcG9zaXRp
b246dG9wIGNlbnRlcjsNCgliYWNrZ3JvdW5kLWNvbG9yOiM3YTZmNTM7Ij4NCjx0YWJsZSB3
aWR0aD0iNjEyIiBib3JkZXI9IjAiIGNlbGxzcGFjaW5nPSIwIiBjZWxscGFkZGluZz0iMCI+
DQogIDx0cj4NCiAgICA8dGQgd2lkdGg9IjUyOSIgYWxpZ249ImxlZnQiPjx0YWJsZSB3aWR0
aD0iMjAwIiBib3JkZXI9IjAiIGNlbGxzcGFjaW5nPSIwIiBjZWxscGFkZGluZz0iMCI+DQog
ICAgICA8dHI+DQogICAgICAgIDx0ZCB3aWR0aD0iOTEiIGFsaWduPS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uPg0KPC9kaXY+DQo8L2NlbnRl
cj4NCg==


--9B095B5ADSN=_01CA638BF4740C5500000045spinstitch.com--

此正则表达式匹配您的 base64 字符串并对其进行解码:

1
2
3
4
5
6
7
8
9
10
11
12
Match m = Regex.Match(
    File.ReadAllText(@"c:\\edit1.txt"),
    @"X-OriginalArrivalTime:.*?\
\
\
\
([\\s\\S]*)\
\
\
\
"
);
byte[] data = Convert.FromBase64String(m.Groups[1].Value);

如果您不想使用正则表达式,您可以尝试跳过每一行,直到找到 X-OriginalArrivalTime 标题。从那里,您可以保留每一行,直到找到空行或流结束。


尝试改用 Read() 方法。