How to get the message thread Id (C# .NET)

We are referring the below link reading the mail message details.

If the message we are reading is part of the conversation, then how do we get the message thread Id?

Here is the code we are using reading the messages from Inbox.

    License license = new License();
    license.SetLicense("Aspose.Total.lic");
	
	ImapClient oClient = new ImapClient();
    oClient.Host = oRow["SMTPSERVER"].ToString().Trim();
    oClient.Username = oRow["USERNAME"].ToString().Trim();
    oClient.Password = oRow["USERPASS"].ToString().Trim();
    oClient.Port = Convert.ToInt32(oRow["SMTPPORT"].ToString().Trim());
    bool bSSLEnabled = Convert.ToBoolean(oRow["ENABLED"].ToString().Trim());
    if (bSSLEnabled)
    {
       oClient.SecurityOptions = SecurityOptions.Auto;
    }

    oClient.SelectFolder(ImapFolderInfo.InBox);

    ImapQueryBuilder builder = new ImapQueryBuilder();

    builder.HasNoFlags(ImapMessageFlags.IsRead);

    MailQuery oQuery = builder.GetQuery();

    ImapMessageInfoCollection messageInfoCol = oClient.ListMessages(oQuery);
	
	for (int i = 0; i < messageInfoCol.Count; i++)
	{
		MailMessage oMsg = oClient.FetchMessage(messageInfoCol[i].UniqueId);
		
		//would like to know how to get the Thread Id of the message from oMsg object. Or any other procedure to get the Thread Id.
	}

@srinudhulipalla,

I have observed your requirements and have not been able to completely understand them. Can you please share elaboration of what actually you want to achieve using Aspose.Email so that we may proceed further to help you.

@mudassir.fayyaz

My mail client is configured to have the mails to show in conversation thread as shown in attached picture. When I am reading the mails using ImapClient object from Aspose.Email, Is there any way I can get conversation id/index? I hope you will be clear now.

image.png (48.0 KB)

@srinudhulipalla,

I request you to please check following sample code. We can use headers from MailMessage for identifying conversation thread.
“References” - The message identifier(s) of other message(s) to which the current
message may be related. This header field contains a list of all Message-IDs
of messages in the preceding reply chain

MailMessage oMsg = oClient.FetchMessage(messageInfoCol[i].UniqueId);

string msgId = oMsg.Headers.Get("Message-ID");
string inReplyTo = oMsg.Headers.Get("In-Reply-To");
string references = oMsg.Headers.Get("References");


Message-ID: (https://tools.ietf.org/html/rfc4021#page-10)
      Contains a single unique message identifier that refers to a
      particular version of a particular message.  If the message is
      resent without changes, the original Message-ID is retained.
      Defined as standard by RFC 822.

In-Reply-To: (https://tools.ietf.org/html/rfc4021#page-10)
      The message identifier(s) of the original message(s) to which the
      current message is a reply.  Defined as standard by RFC 822.

References: (https://tools.ietf.org/html/rfc4021#page-11)
      The message identifier(s) of other message(s) to which the current
      message may be related.  In RFC 2822, the definition was changed
      to say that this header field contains a list of all Message-IDs
      of messages in the preceding reply chain.  Defined as standard by
      RFC 822.

Conversation example:

1 > SEND >
Message-ID: <id001@host.com>

2 < RECEIVE <
Message-ID: <id002@host.com>
In-Reply-To: <id001@host.com>
References: <id001@host.com>

3 > SEND >
Message-ID: <id003@host.com>
In-Reply-To: <id002@host.com>
References: <id001@host.com>
 <id002@host.com>

4 < RECEIVE <
Message-ID: <id004@host.com>
In-Reply-To: <id003@host.com>
References: <id001@host.com>
 <id002@host.com> 
 <id003@host.com>

Please also check following links: