Retrieving the Message-ID - Rebex Q&A Forum (C#, VB.NET) most recent 30 from http://forum.rebex.net2010-09-08T14:19:00Zhttp://forum.rebex.net/feeds/question/96http://www.creativecommons.org/licenses/by-nc/2.5/rdfhttp://forum.rebex.net/questions/96/retrieving-the-message-idRetrieving the Message-IDWakatake2010-03-07T14:11:32Z2010-08-27T01:23:57Z
<p>Hello,</p>
<p>How do I parse the Message-ID from the headers?</p>
<p>I have the following:</p>
<pre><code>static void Main(string[] args)
{
Imap imap = new Imap();
imap.Connect("imap.gmail.com", 993, null, ImapSecurity.Implicit);
imap.Login(username, password);
imap.SelectFolder("Inbox");
ImapMessageCollection collection = imap.GetMessageList(ImapListFields.FullHeaders);
foreach (ImapMessageInfo msg in collection)
{
Console.WriteLine(msg.MessageId.Id);
}
Console.Read();
}
</code></pre>
<p>but I am having a NullReferenceException. How do I go about it, the correct way? Thanks!</p>
http://forum.rebex.net/questions/96/retrieving-the-message-id/98#98Answer by Martin Vobr for Retrieving the Message-IDMartin Vobr2010-03-08T11:39:18Z2010-03-08T11:39:18Z<p>Hello,</p>
<p>the <code>Message-ID</code> header is optional and it looks like you have found a message with missing <code>Message-ID</code> header. </p>
<p>See following quote from the <a href="http://www.rfc-editor.org/rfc/rfc5322.txt" rel="nofollow">RFC 5322</a>. </p>
<blockquote>
<p>3.6.4. Identification Fields </p>
<p>Though listed as optional in the table in section 3.6, every
message SHOULD have a "Message-ID:"
field.</p>
</blockquote>
<p>It means that when creating a new message you should include the <code>Message-ID</code> field, but also that every parser should be able to process messages with <code>Message-ID</code> not set.</p>
<p>Try using this code:</p>
<pre><code>foreach (ImapMessageInfo msg in collection)
{
if (msg.MessageId != null)
Console.WriteLine(msg.MessageId.Id);
else
Console.WriteLine("Message has no Message-ID header set.");
}
</code></pre>