Representation of a Mail.app mail message

Hi,
I am writing a small script that takes all the messages I have in a local folder on my Mac and generates an mbox file with mime messages. When there are attachments the message is generated on a multipart/mixed content type with the text first and the attachments following.
I noticed that Mail.app gives me in the content of each message the text of the previously sent message and other chains of message texts in the conversation. And in each message I have access to the attachments of that message but not the attachments of the messages previously in the conversation.
Mail.app shows the conversation by getting the previous messages from the sent folder as well as the other messages in the Mailbox and presents them in sequence with attachments.

Is it possible to, having a mail message get the related messages of it either in the sent folder or in the same folder in order to build a multipart/mixed messages representation in chronological order with the attachments interspersed between the corresponding text ?

Thanks,
Regards

The mail system puts the headers in the message :
Message-ID, References and In-Reply-To.
I can see them in mail messages exported from Mail.app.
So the script will have to do a search on the mailbox where the message is in and the sent folder of the account on which it was received to correlate the messages.
Mail.app seems to do that on its own but does not expose it to AppleScript.

Yeah, you hit the nail on the head there—since Mail.app hides those threading relationships under the hood, you pretty much have to parse the In-Reply-To and References headers yourself and query both the mailbox and the sent folder to stitch a proper chronological conversation together. It’s a bit of a headache, but looping through the message IDs is usually the only way to get it sorted out cleanly.

I hope this was useful.

I ended up thinking about the subject better, and came to the conclusion that I will just create the Mbox files with a direct mapping from the local folders from where I am exporting, and create an index file with the message correlation ids so I can search for them.
Its too expensive to include in the exporting process the search for the correlated messages.