jstedfast/MailKit · error · ArgumentException

The number of messages and the number of dates must be…

Error message

The number of messages and the number of dates must be equal.

What it means

Append also accepts per-message internal dates and validates that dates.Count equals messages.Count, since each AppendRequest carries one DateTimeOffset. A mismatch throws this ArgumentException after the flags checks pass.

Solutions

  1. Derive dates from the same collection: messages.Select (m => m.Date).ToList ().
  2. If a uniform date is fine, use messages.Count copies of that DateTimeOffset.
  3. Use the Append overload without dates (server assigns current time) if internal dates are not needed.

Example fix

// before
folder.Append (messages, flags, datesForSomeMessages);

// after
var dates = messages.Select (m => m.Date).ToList ();
folder.Append (messages, flags, dates);
Defensive patterns

Strategy: validation

Validate before calling

if (dates == null || dates.Count != messages.Count)
    dates = messages.Select (m => m.Date.DateTime).ToList ();

Try / catch

try {
    folder.Append (messages, flags, dates);
} catch (ArgumentException ex) when (ex.Message.Contains ("number of dates")) {
    logger.LogError (ex, "dates count {0} != messages count {1}", dates?.Count, messages?.Count);
}

Prevention

When it happens

Trigger: Calling the Append overload with dates where dates.Count != messages.Count — e.g. dates for a subset, or a single date reused for all.

Common situations: Reconstructing messages with original IMAP internal dates but collecting dates only for messages that had a Date header; mismatched batch sizes between messages and dates pipelines.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at MailKit/IMailFolderAppendExtensions.cs:1126

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

			for (int i = 0; i < messages.Count; i++) {
				if (messages[i] == null)
					throw new ArgumentException ("One or more of the messages is null.");
			}

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

			if (messages.Count != flags.Count)
				throw new ArgumentException ("The number of messages and the number of flags must be equal.");

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

			if (messages.Count != dates.Count)
				throw new ArgumentException ("The number of messages and the number of dates must be equal.");

			var requests = new AppendRequest[messages.Count];
			for (int i = 0; i < messages.Count; i++) {
				requests[i] = new AppendRequest (messages[i], flags[i], dates[i]) {
					TransferProgress = progress
				};
			}

			return folder.Append (options, requests, cancellationToken);
		}

		/// <summary>
		/// Asynchronously append the specified messages to the folder.
		/// </summary>
		/// <remarks>
		/// Asynchronously appends the specified messages to the folder and returns the UniqueIds assigned to the messages.
		/// </remarks>
		/// <returns>The UIDs of the appended messages, if available; otherwise an empty array.</returns>

View on GitHub (pinned to 9d3859a785)