MailKit : search if custom header exists

1.2k views Asked by At

I have added a custom header in emails, conditionally

and I want using the IMAP/SearchQuery to return only the mails having the header.

Not its value, only to return true/false if the header exists.

Is it possible ? thanks.

1

There are 1 answers

1
jstedfast On BEST ANSWER

According to rfc3501 (the IMAP specification):

HEADER <field-name> <string>
         Messages that have a header with the specified field-name (as
         defined in [RFC-2822]) and that contains the specified string
         in the text of the header (what comes after the colon).  If the
         string to search is zero-length, this matches all messages that
         have a header line with the specified field-name regardless of
         the contents.

To make this search in MailKit, what you would do is this:

var results = folder.Search (SearchQuery.Header ("X-Custom-Header", string.Empty));

Edit:

I just discovered a logic bug in MailKit that would decide to send empty strings as ATOMs instead of QSTRING like it should. I've fixed this in git master, but have not made a release yet. I'll probably do that this weekend.