jstedfast/MailKit · error · ArgumentNullException

Value cannot be null. (Parameter 'uids')

Error message

Value cannot be null. (Parameter 'uids')

What it means

ImapFolder.Expunge(IList<UniqueId>, ...) validates its uids argument before doing anything; a null list is rejected with ArgumentNullException. Note that an empty (non-null) list is accepted and is a no-op.

Solutions

  1. Pass a non-null IList<UniqueId>; pass an empty list (or use the parameterless Expunge()) to expunge nothing.
  2. Guard with ?? new List<UniqueId>() or an early null check at the call site.
  3. If you only want to expunge all flagged-deleted messages, call Expunge() without the uids argument.

Example fix

// before
folder.Expunge (uids);
// after
folder.Expunge (uids ?? new List<UniqueId> ());
Defensive patterns

Strategy: validation

Validate before calling

if (uids == null) throw new InvalidOperationException ("uids must be provided; pass an empty list to expunge nothing");

Type guard

IList<UniqueId> SafeUids (IList<UniqueId> u) => u ?? new List<UniqueId> ();

Try / catch

try { folder.Expunge (uids); } catch (ArgumentNullException ex) when (ex.ParamName == "uids") { /* supply a valid list */ }

Prevention

When it happens

Trigger: Calling Expunge(null) directly, or passing the result of a method/lookup that returned null instead of an empty collection.

Common situations: Storing the UID list in a variable that was never initialized; a search/annotation lookup returning null on failure and being fed straight into Expunge.

Related errors


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

Appendix: source

Thrown at MailKit/Net/Imap/ImapFolder.cs:4268

		/// <exception cref="FolderNotOpenException">
		/// The <see cref="ImapFolder"/> is not currently open in read-write mode.
		/// </exception>
		/// <exception cref="System.OperationCanceledException">
		/// The operation was canceled via the cancellation token.
		/// </exception>
		/// <exception cref="System.IO.IOException">
		/// An I/O error occurred.
		/// </exception>
		/// <exception cref="ImapProtocolException">
		/// The server's response contained unexpected tokens.
		/// </exception>
		/// <exception cref="ImapCommandException">
		/// The server replied with a NO or BAD response.
		/// </exception>
		public override void Expunge (IList<UniqueId> uids, CancellationToken cancellationToken = default)
		{
			if (uids == null)
				throw new ArgumentNullException (nameof (uids));

			CheckState (true, true);

			if (uids.Count == 0)
				return;

			if ((Engine.Capabilities & ImapCapabilities.UidPlus) == 0) {
				// get the list of messages marked for deletion that should not be expunged
				var query = SearchQuery.Deleted.And (SearchQuery.Not (SearchQuery.Uids (uids)));
				var unmark = Search (SearchOptions.None, query, cancellationToken);

				if (unmark.Count > 0) {
					// clear the \Deleted flag on all messages except the ones that are to be expunged
					Store (unmark.UniqueIds, RemoveDeletedFlag, cancellationToken);
				}

				// expunge the folder
				Expunge (cancellationToken);

View on GitHub (pinned to 9d3859a785)