jstedfast/MailKit · error · ArgumentException
Cannot search for an empty string.
Error message
Cannot search for an empty string.
What it means
SearchQuery.HasGMailLabel(string label) rejects an empty-string label with ArgumentException("Cannot search for an empty string.") because IMAP/GMail label searches require non-empty text; an empty term would produce an invalid search key.
Solutions
- Check string.IsNullOrEmpty(label) before calling HasGMailLabel and skip the query when empty.
- Trim and validate user/config-supplied label values at input time.
- Filter out empty entries when building labels from a list.
Example fix
// before var query = SearchQuery.HasGMailLabel(label); // label may be "" // after var trimmed = label?.Trim(); var query = string.IsNullOrEmpty(trimmed) ? null : SearchQuery.HasGMailLabel(trimmed);
Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(label)) throw new ArgumentException("GMail label cannot be empty.", nameof(label));
var query = SearchQuery.HasGMailLabel(label.Trim()); Type guard
static bool IsValidLabel(string label) => !string.IsNullOrWhiteSpace(label);
Try / catch
try { var q = SearchQuery.HasGMailLabel(label); } catch (ArgumentException) { /* skip label filter */ } Prevention
- Trim and reject blank labels at input time.
- Skip empty label entries when building queries from lists.
- Require non-empty label fields in configuration schemas.
When it happens
Trigger: Calling SearchQuery.HasGMailLabel("") — usually from a config value that is present but blank, or string.Split/trimming results yielding "".
Common situations: Blank label entries in settings files; user leaving a label field empty in UI; parsing label lists where an empty token appears.
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
- Cannot search for an empty string.
- The host name cannot be empty.
- The language code cannot be empty.
- A metadata tag identifier cannot be empty.
- Annotation attribute specifiers cannot be empty.
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/d2303dbae9900870.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/Search/SearchQuery.cs:1179
/// for use with IMAP servers that support the <c>X-GM-EXT1</c> extension.</note>
/// <seealso cref="MailKit.Net.Imap.ImapCapabilities.GMailExt1"/>
/// <seealso cref="MailKit.Net.Imap.ImapClient.Capabilities"/>
/// </remarks>
/// <returns>A <see cref="TextSearchQuery"/>.</returns>
/// <param name="label">The GMail label.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="label"/> is <see langword="null" />.
/// </exception>
/// <exception cref="System.ArgumentException">
/// <paramref name="label"/> is empty.
/// </exception>
public static TextSearchQuery HasGMailLabel (string label)
{
if (label == null)
throw new ArgumentNullException (nameof (label));
if (label.Length == 0)
throw new ArgumentException ("Cannot search for an empty string.", nameof (label));
return new TextSearchQuery (SearchTerm.GMailLabels, label);
}
/// <summary>
/// Match messages using the GMail search expression.
/// </summary>
/// <remarks>
/// <para>Matches messages using Google's custom message search syntax.</para>
/// <note type="note">This is equivalent to the <c>X-GM-RAW</c> search key as defined in <a href="https://developers.google.com/gmail/imap_extensions">Google's IMAP extensions</a> and is therefor only available
/// for use with IMAP servers that support the <c>X-GM-EXT1</c> extension.</note>
/// <seealso cref="MailKit.Net.Imap.ImapCapabilities.GMailExt1"/>
/// <seealso cref="MailKit.Net.Imap.ImapClient.Capabilities"/>
/// </remarks>
/// <returns>A <see cref="TextSearchQuery"/>.</returns>
/// <param name="expression">The raw GMail search text.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="expression"/> is <see langword="null" />.View on GitHub (pinned to 9d3859a785)