jstedfast/MailKit · error · ArgumentOutOfRangeException

count

Error message

count

What it means

MailFolder.GetStream(UniqueId uid, BodyPart part, int offset, int count, ...) throws ArgumentOutOfRangeException with the message "count" when a negative number of bytes to read is supplied. The count must be zero or positive because it defines the length of the partial IMAP fetch; a negative value would produce an invalid IMAP body-section range.

Solutions

  1. Validate count >= 0 before calling; clamp to the remaining stream length
  2. Use Math.Max(0, (int)(totalBytes - offset)) for remaining-bytes calculations
  3. If you want the whole part, call the GetStream overload without offset/count rather than a negative count

Example fix

// before
stream = folder.GetStream(uid, part, offset, total - offset); // underflows
// after
var count = Math.Max(0, (int)(total - offset));
stream = folder.GetStream(uid, part, offset, count);
Defensive patterns

Strategy: validation

Validate before calling

if (count < 0) throw new ArgumentException("count must be >= 0", nameof(count));

Type guard

static bool IsValidCount(int count) => count >= 0;

Try / catch

try {
    var stream = folder.GetStream(uid, part, offset, count);
} catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "count") {
    // recompute count from part size
}

Prevention

When it happens

Trigger: Calling MailFolder.GetStream(uid, part, offset, count) with count < 0, commonly from computing count as remaining = total - offset when offset > total, or passing an unvalidated chunkSize variable.

Common situations: Chunked download logic where the last chunk calculation underflows; passing -1 as a sentinel for 'read everything' (not supported; use the overload without offset/count instead).

Related errors


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

Appendix: source

Thrown at MailKit/MailFolder.cs:6215

		/// <exception cref="ProtocolException">
		/// The server's response contained unexpected tokens.
		/// </exception>
		/// <exception cref="CommandException">
		/// The command failed.
		/// </exception>
		public virtual Stream GetStream (UniqueId uid, BodyPart part, int offset, int count, CancellationToken cancellationToken = default, ITransferProgress? progress = null)
		{
			if (!uid.IsValid)
				throw new ArgumentException ("The uid is invalid.", nameof (uid));

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

			if (offset < 0)
				throw new ArgumentOutOfRangeException (nameof (offset));

			if (count < 0)
				throw new ArgumentOutOfRangeException (nameof (count));

			return GetStream (uid, part.PartSpecifier, offset, count, cancellationToken, progress);
		}

		/// <summary>
		/// Asynchronously get a substream of the specified body part.
		/// </summary>
		/// <remarks>
		/// Asynchronously gets a substream of the body part. If the starting offset is beyond
		/// the end of the body part, an empty stream is returned. If the number of
		/// bytes desired extends beyond the end of the body part, a truncated stream
		/// will be returned.
		/// </remarks>
		/// <returns>The stream.</returns>
		/// <param name="uid">The UID of the message.</param>
		/// <param name="part">The desired body part.</param>
		/// <param name="offset">The starting offset of the first desired byte.</param>
		/// <param name="count">The number of bytes desired.</param>

View on GitHub (pinned to 9d3859a785)