jstedfast/MailKit · error · ArgumentNullException

Value cannot be null. (Parameter 'tags')

Error message

Value cannot be null. (Parameter 'tags')

What it means

The multi-tag QueueGetMetadataCommand throws ArgumentNullException when the tags collection is null. At least one metadata tag must be requested for GETMETADATA, so a null enumeration is rejected immediately with the parameter name.

Solutions

  1. Pass a non-empty collection of MetadataTag values (an empty list is fine; null is not)
  2. Coalesce null to an empty list: tags ?? Enumerable.Empty<MetadataTag>()
  3. Validate the collection before calling

Example fix

// before
List<MetadataTag> tags = BuildTags(); // may return null
folder.GetMetadata(options, tags, ct);
// after
var tags = BuildTags() ?? new List<MetadataTag>();
folder.GetMetadata(options, tags, ct);
Defensive patterns

Strategy: validation

Validate before calling

if (tags == null || tags.Any(t => t == null))
    throw new ArgumentException("Tags collection must be non-null with valid tags", nameof(tags));

Type guard

bool HasTags(IEnumerable<MetadataTag> t) => t != null;

Try / catch

try {
    var values = folder.GetMetadata(options, tags, ct);
} catch (ArgumentNullException ex) {
    logger.LogError(ex, "Null tags passed to GetMetadata");
    throw;
}

Prevention

When it happens

Trigger: Calling ImapFolder.GetMetadata(MetadataOptions, IEnumerable<MetadataTag>, CancellationToken) with a null tags argument (uninitialized list, null return from a helper).

Common situations: Building the tag list conditionally and forgetting to default it; passing a null result of a config lookup straight into GetMetadata.

Related errors


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

Appendix: source

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

		/// <exception cref="ImapCommandException">
		/// The server replied with a NO or BAD response.
		/// </exception>
		public override async Task<string?> GetMetadataAsync (MetadataTag tag, CancellationToken cancellationToken = default)
		{
			var ic = QueueGetMetadataCommand (tag, cancellationToken);

			await Engine.RunAsync (ic).ConfigureAwait (false);

			return ProcessGetMetadataResponse (ic, tag);
		}

		ImapCommand? QueueGetMetadataCommand (MetadataOptions options, IEnumerable<MetadataTag> tags, CancellationToken cancellationToken)
		{
			if (options == null)
				throw new ArgumentNullException (nameof (options));

			if (tags == null)
				throw new ArgumentNullException (nameof (tags));

			CheckState (false, false);

			if ((Engine.Capabilities & ImapCapabilities.Metadata) == 0)
				throw new NotSupportedException ("The IMAP server does not support the METADATA extension.");

			var command = new StringBuilder ("GETMETADATA %F");
			var args = new List<object> ();
			bool hasOptions = false;

			if (options.MaxSize.HasValue || options.Depth != 0) {
				command.Append (" (");
				if (options.MaxSize.HasValue) {
					command.Append ("MAXSIZE ");
					command.Append (options.MaxSize.Value.ToString (CultureInfo.InvariantCulture));
					command.Append (' ');
				}
				if (options.Depth > 0) {

View on GitHub (pinned to 9d3859a785)