jstedfast/MailKit · error · ArgumentException

Cannot search for an invalid unique identifier.

Error message

Cannot search for an invalid unique identifier.

What it means

The UidSearchQuery(UniqueId) constructor requires a valid UID; MailKit represents an invalid/unassigned UID (Id == 0) with UniqueId.IsValid == false. Searching for such a UID cannot be translated to an IMAP UID SEARCH command, so the constructor throws ArgumentException immediately.

Solutions

  1. Check uid.IsValid before constructing the query and skip or handle invalid UIDs separately.
  2. Verify the code path that produced the UniqueId actually assigns it from a fetched message (e.g., message.UniqueId from Fetch(UniqueId, ...)).
  3. For persisted UIDs, validate that stored Id/Validity values are non-zero before rebuilding a UniqueId.

Example fix

// before
folder.Search(new UidSearchQuery(uid)); // throws if uid invalid

// after
if (uid.IsValid)
    folder.Search(new UidSearchQuery(uid));
Defensive patterns

Strategy: validation

Validate before calling

if (uid.IsValid)
    folder.Search(SearchQuery.Uids(uid));

Type guard

bool IsValidUid(UniqueId uid) => uid != default && uid.IsValid;

Try / catch

try {
    folder.Search(new UidSearchQuery(uid));
} catch (ArgumentException ex) when (ex.ParamName == "uid") {
    // invalid UID: log and skip, or re-fetch the message to obtain a valid UID
}

Prevention

When it happens

Trigger: Calling new UidSearchQuery(uid) where uid.IsValid is false — typically a default(UniqueId) (Id 0, Validity 0) or a UID obtained from a failed/uninitialized operation.

Common situations: Passing UniqueId.MinValue or default(UniqueId) obtained from an uninitialized field; mapping code that stores UIDs in a DB and reads back zero/invalid placeholders; using UIDs parsed from user input that was never validated.

Related errors


AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15). Data as JSON: /api/errors/9b8d0a4c6fad3414. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/Search/UidSearchQuery.cs:77

				throw new ArgumentException ("Cannot search for an empty set of unique identifiers.", nameof (uids));

			Uids = uids;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="T:MailKit.Search.UidSearchQuery"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new unique identifier-based search query.
		/// </remarks>
		/// <param name="uid">The unique identifier to match against.</param>
		/// <exception cref="System.ArgumentException">
		/// <paramref name="uid"/> is an invalid unique identifier.
		/// </exception>
		public UidSearchQuery (UniqueId uid) : base (SearchTerm.Uid)
		{
			if (!uid.IsValid)
				throw new ArgumentException ("Cannot search for an invalid unique identifier.", nameof (uid));

			Uids = new UniqueIdSet (SortOrder.Ascending) {
				uid
			};
		}

		/// <summary>
		/// Gets the unique identifiers to match against.
		/// </summary>
		/// <remarks>
		/// Gets the unique identifiers to match against.
		/// </remarks>
		/// <value>The unique identifiers.</value>
		public new IList<UniqueId> Uids {
			get; private set;
		}
	}
}

View on GitHub (pinned to 9d3859a785)