在StreamReader中搜索base64编码的内容?

时间:2009-11-12 20:32:29

标签: c# .net-3.5 parsing base64

我需要从IIS解析来自SMTP badmail文件夹的NDR文件。附加到NDR的是base64编码的内容。这就是我需要达到的目标。我希望,使用StreamReader,它将是一行,但是当一个连续的.ReadLine时,它们是分开的行。

以下是NDR的示例:

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
aHR0cDovL3NlcnZpY2VzLnNwaW5zdGl0Y2guY29t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qd0tORmlBSGdiRWxBeHBTNnlFUHF0WC5qcGciIG5h
bWU9ImZseWVyQmciIHdpZHRoPSI2MTIiIGhlaWdodD0iNzkyIiBzdHlsZT0iYm9yZGVyOiAx
cHggc29saWQ7Ij48L3RkPg0KICA8L3RyPg0KPC90YWJsZT4NCjxicj4NCjxhIGlkPSJ1bnN1
YiIgc3R5bGU9ImZvbnQtZmFtaWx5OiBBcmlhbCwgSGVsdmV0aWNhLCBzYW5zLXNlcmlmOyBm
b250LXNpemU6IDEwcHg7IiBocmVmPSJodHRwOi8vc2VydmljZXMuc3BpbnN0aXRjaC5jb20v
TWFpbERpcmVjdC5hc3B4P21haWxlcj1LMGl4NHAyZGdXOG96M0ZmQTRORnJicVZhaFE2aTln
dmdmZWZlVnI4UmZFYWpnZ2phWFJ0TjFlNkVteThuYnNHVG1jMThKNFJ5YTElMmY1WVFvaFhr
aFA2VTNFcTRFRHB4R1J4YmpjeXlhOG56aExKT2hDM0QzJTJmdFY1WHpmbFY2SmElN2N1bnN1
YiI+VW5zdWJzY3JpYmUgZnJvbSB0aGlzIG1haWxpbmc8L2E+PGJyPg0KPHNwYW4gc3R5bGU9
ImZvbnQtZmFtaWx5OiBBcmlhbCwgSGVsdmV0aWNhLCBzYW5zLXNlcmlmOyBmb250LXNpemU6
IDEwcHg7Ij5Db3B5cmlnaHQgw4LCqSAyMDA5IFNwaW5TdGl0Y2ggTExDIDExMTAgQmFzaWxk
b24gUmQuIE10LiBQbGVhc2FudCwgU0MgIDI5NDY2PC9zcGFuPg0KPC9kaXY+DQo8L2NlbnRl
cj4NCg==


--9B095B5ADSN=_01CA638BF4740C5500000045spinstitch.com--

2 个答案:

答案 0 :(得分:1)

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

Match m = Regex.Match(
    File.ReadAllText(@"c:\edit1.txt"),
    @"X-OriginalArrivalTime:.*?\r\n\r\n([\s\S]*)\r\n\r\n");
byte[] data = Convert.FromBase64String(m.Groups[1].Value);

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

答案 1 :(得分:0)

请尝试使用Read()方法。