You can use IMail.MessageId and IMail.References and IMail.InReplyTo fields for that:
- IMail.MessageId - Message-ID header is an ID of the current message.
- IMail.References - References header contains IDs of all referenced emails.
- IMail.InReplyTo - In-Reply-To header contains ID of the message to which this message is reply.
However, bear in mind that not all email programs set those correctly.
Consider additionally adding something like "[TicketId: 1234]" in the subject of the email when you create a reply.
Most email programs don't modify subject when replying (apart from adding additional "RE: " at the begging).
I think the best way is to use both approaches simultaneously to thread emails reliably.