是否可以使用 MimeKit.MimeParser 从 mbox 文件中读取第 M 条消息的第 N 个附件?就我而言,我会将少量消息(每个消息的几个字段,包括附件列表)存储到内存数据结构中,之后,我希望能够返回到特定的消息附件并阅读其内容。
到目前为止我尝试过的事情:
- 记住每条读取消息的底层流位置并将流定位到该位置,然后再调用 _parser.ParseMessage() 以获取消息及其附件。
- 我还尝试使用 LINQ 方法通过 MessageID 获取消息,并结合将流位置设置为 0 并再次调用 SetStream 而没有它。
以上不起作用。
这里有一些代码只是为了说明我的努力:
public void SaveAttachment(Attachment att, Stream outStream)
{
_inputStream.Seek(0, SeekOrigin.Begin);
_parser.SetStream(_inputStream, false);
//MimeMessage mimeMsg = _parser.Skip((int)(att.Parent as Message).Position).First();
MimeMessage mimeMsg =_parser.SingleOrDefault(x => x.MessageId == (att.Parent as Message).EntryID);
MimeEntity mimeAtt = mimeMsg.Attachments.ToList()[att.AttachmentIndex];
if (mimeAtt is MessagePart)
{
(mimeAtt as MessagePart).Message.WriteTo(outStream);
}
else
{
(mimeAtt as MimePart).Content.DecodeTo(outStream);
}
}