jstedfast/MailKit · error · ArgumentException
No flags specified.
Error message
No flags specified.
What it means
SearchQuery.HasFlags builds an IMAP FLAGS search query by AND-ing one sub-query per MessageFlags bit set in the argument. If no recognized bits are set, there is no query to build, so it throws ArgumentException. This guards against silently producing an empty/invalid search term.
Solutions
- Ensure at least one MessageFlags bit (e.g. MessageFlags.Seen, Draft, Recent, Flagged, Answered, Deleted) is set before calling HasFlags.
- Check the value with `if (flags != MessageFlags.None) query = SearchQuery.HasFlags(flags);` and otherwise skip the flag criterion.
- Verify any cast/calculation producing the MessageFlags value actually maps intended bits onto the MailKit enum.
Example fix
// before
var query = SearchQuery.HasFlags(unreadFlags); // unreadFlags == 0
// after
var query = unreadFlags != MessageFlags.None
? SearchQuery.HasFlags(unreadFlags)
: SearchQuery.All; Defensive patterns
Strategy: validation
Validate before calling
if (flags == MessageFlags.None)
throw new ArgumentException("At least one MessageFlags bit must be set.", nameof(flags));
var query = SearchQuery.HasFlags(flags); Type guard
bool HasValidFlags(MessageFlags flags) => flags != MessageFlags.None;
Try / catch
try { query = SearchQuery.HasFlags(flags); }
catch (ArgumentException ex) { log.Warn(ex.Message); query = SearchQuery.All; } Prevention
- Never pass default(MessageFlags); initialize flag accumulators to a meaningful combination or None and branch on it.
- Wrap raw casts to MessageFlags in a parse function that returns None-safe results.
- Unit-test search builders with a zero-flags case.
When it happens
Trigger: Calling SearchQuery.HasFlags with a MessageFlags value of 0 (e.g. (MessageFlags)0, a default-struct value, or an enum cast that lost all bits).
Common situations: Building search criteria programmatically where flags were accumulated in a variable that was never assigned; casting an int/enum from user config that maps to no MessageFlags bit; default(MessageFlags) passed by mistake.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
Related errors
- The filter name cannot be empty.
- The keyword cannot be an empty string.
- No keywords specified.
- array
- uri
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/877e1cfe5f4f592f.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/Search/SearchQuery.cs:418
public static SearchQuery HasFlags (MessageFlags flags)
{
var list = new List<SearchQuery> ();
if ((flags & MessageFlags.Seen) != 0)
list.Add (Seen);
if ((flags & MessageFlags.Answered) != 0)
list.Add (Answered);
if ((flags & MessageFlags.Flagged) != 0)
list.Add (Flagged);
if ((flags & MessageFlags.Deleted) != 0)
list.Add (Deleted);
if ((flags & MessageFlags.Draft) != 0)
list.Add (Draft);
if ((flags & MessageFlags.Recent) != 0)
list.Add (Recent);
if (list.Count == 0)
throw new ArgumentException ("No flags specified.", nameof (flags));
var query = list[0];
for (int i = 1; i < list.Count; i++)
query = query.And (list[i]);
return query;
}
/// <summary>
/// Match messages that do not have any of the specified flags set.
/// </summary>
/// <remarks>
/// <para>Matches messages that do not have any of the specified flags set.</para>
/// <note type="note">Maps each flag to the corresponding search key (<c>UNANSWERED</c>, <c>UNDELETED</c>, <c>UNDRAFT</c>, <c>UNFLAGGED</c>,
/// <c>OLD</c> or <c>UNSEEN</c>) as defined in <a href="https://datatracker.ietf.org/doc/html/rfc3501#section-6.4.4">rfc3501</a>.</note>
/// </remarks>
/// <returns>A <see cref="SearchQuery"/>.</returns>
/// <param name="flags">The message flags.</param>View on GitHub (pinned to 9d3859a785)