Is it possible to read the Nth attachment of the Mth message, from an mbox file, using MimeKit.MimeParser? In my case, I would store few messages (few fields for each msg including a list of attachments) to an in-memory data structure and after that, I want to be able to return to a specific message attachment and read its contents.
Things I have tried so far:
- Remembering underlying stream position for each read message and positioning the stream to that position before calling _parser.ParseMessage() later to get the message and its attachment.
- I also tried to use LINQ methods to get a message by MessageID in combination with setting stream position to 0 and calling SetStream again and without it.
The above does not work.
Here is some code just to illustrate my efforts:
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);
}
}