+1 vote
by (240 points)
edited

Hello,

How do I parse the Message-ID from the headers?

I have the following:

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();
}

but I am having a NullReferenceException. How do I go about it, the correct way? Thanks!

Applies to: Rebex Secure Mail

1 Answer

0 votes
by (13.3k points)
edited

Hello,

the Message-ID header is optional and it looks like you have found a message with missing Message-ID header.

See following quote from the RFC 5322.

3.6.4. Identification Fields

Though listed as optional in the table in section 3.6, every message SHOULD have a "Message-ID:" field.

It means that when creating a new message you should include the Message-ID field, but also that every parser should be able to process messages with Message-ID not set.

Try using this 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.");
}
...