从解码的 base64 MIME 部分获取 Byte[]
Getting Byte[] from Decoded base64 MIME Part
我们从各种实体接收文件,这些文件中可能包含 base64 编码的 MIME 部分(即 PDF)。我们将其提取出来并存储起来以备后用。这对除其中一个实体以外的所有人都有效。他们的文件将保存,但无法打开。
代码如下:
public byte[] GetAttachmentByName(string name)
{
foreach (var attachment in _mimeMessage.BodyParts.OfType<MimePart>())
{
if (attachment.ContentId != name)
continue;
using (var stream = new System.IO.MemoryStream())
{
using (var filtered = new FilteredStream(stream))
{
filtered.Add(DecoderFilter.Create("base64"));
attachment.ContentObject.DecodeTo(filtered);
return stream.ToArray();
}
}
}
return null;}
这是 MIME 的一部分:
MIME 版本:1.0
内容类型:multipart/related; boundary="=-antAW7IrKfe1DvH3559M9g=="
…
--=-antAW7IrKfe1DvH3559M9g==
内容类型:application/pdf
内容传输编码:base64
内容描述:
内容 ID:"AC13127925.pdf"
…
bGVyDTw8IA0vU2l6ZSAxODkgDS9Sb290IDE4OCAwIFIgDS9JbmZvIDEgMCBSIA0+PiANc3Rh
cnR4cmVmDTU3MzU3MiANJSVFT0YN
--=-antAW7IrKfe1DvH3559M9g==--
(我包括了 MIME 部分的开头、中间和结尾。文件的另一部分也有 XML,但处理得很好。我们遇到的问题是MIME/PDF.)
感谢任何指导。
MimeContent.DecodeTo()
方法已经为您解码了 base64,因此您需要将代码更改为:
public byte[] GetAttachmentByName(string name)
{
foreach (var attachment in _mimeMessage.BodyParts.OfType<MimePart>())
{
if (attachment.ContentId != name)
continue;
using (var stream = new System.IO.MemoryStream())
{
attachment.ContentObject.DecodeTo(filtered);
return stream.ToArray();
}
}
return null;
}
我们从各种实体接收文件,这些文件中可能包含 base64 编码的 MIME 部分(即 PDF)。我们将其提取出来并存储起来以备后用。这对除其中一个实体以外的所有人都有效。他们的文件将保存,但无法打开。
代码如下:
public byte[] GetAttachmentByName(string name)
{
foreach (var attachment in _mimeMessage.BodyParts.OfType<MimePart>())
{
if (attachment.ContentId != name)
continue;
using (var stream = new System.IO.MemoryStream())
{
using (var filtered = new FilteredStream(stream))
{
filtered.Add(DecoderFilter.Create("base64"));
attachment.ContentObject.DecodeTo(filtered);
return stream.ToArray();
}
}
}
return null;}
这是 MIME 的一部分:
MIME 版本:1.0 内容类型:multipart/related; boundary="=-antAW7IrKfe1DvH3559M9g=="
…
--=-antAW7IrKfe1DvH3559M9g== 内容类型:application/pdf 内容传输编码:base64 内容描述: 内容 ID:"AC13127925.pdf"
…
bGVyDTw8IA0vU2l6ZSAxODkgDS9Sb290IDE4OCAwIFIgDS9JbmZvIDEgMCBSIA0+PiANc3Rh cnR4cmVmDTU3MzU3MiANJSVFT0YN
--=-antAW7IrKfe1DvH3559M9g==--
(我包括了 MIME 部分的开头、中间和结尾。文件的另一部分也有 XML,但处理得很好。我们遇到的问题是MIME/PDF.)
感谢任何指导。
MimeContent.DecodeTo()
方法已经为您解码了 base64,因此您需要将代码更改为:
public byte[] GetAttachmentByName(string name)
{
foreach (var attachment in _mimeMessage.BodyParts.OfType<MimePart>())
{
if (attachment.ContentId != name)
continue;
using (var stream = new System.IO.MemoryStream())
{
attachment.ContentObject.DecodeTo(filtered);
return stream.ToArray();
}
}
return null;
}