jstedfast/MailKit · error · NotSupportedException

The stream does not support resizing.

Error message

The stream does not support resizing.

What it means

ProgressStream.SetLength always throws NotSupportedException with "The stream does not support resizing." A progress-reporting wrapper over a read-only source stream has no mechanism to change the underlying stream's length, so resizing is explicitly rejected.

Solutions

  1. Do not resize a ProgressStream; it is for reading/monitoring only
  2. Perform SetLength on the underlying source stream directly if it genuinely supports it
  3. Check CanWrite/CanSeek before attempting resize operations

Example fix

// before
progressStream.SetLength(0);
// after
if (source.CanWrite)
    source.SetLength(0);
else
    throw new NotSupportedException("The wrapped source stream cannot be resized");
Defensive patterns

Strategy: type-guard

Type guard

bool CanResize(Stream s) => s.CanWrite && s.CanSeek; // ProgressStream never satisfies this for SetLength

Try / catch

try { stream.SetLength(0); }
catch (NotSupportedException) { /* resize the underlying source instead */ }

Prevention

When it happens

Trigger: Calling stream.SetLength(n) on a ProgressStream; indirectly via APIs that truncate streams (e.g. File stream helpers, SetLength called by writer classes).

Common situations: Treating the progress-wrapped network stream like a writable file stream; generic stream-handling utility code that calls SetLength on all Stream instances.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at MailKit/ProgressStream.cs:182

			if (cancellable != null)
				cancellable.Flush (cancellationToken);
			else
				Source.Flush ();
		}

		public override void Flush ()
		{
			Source.Flush ();
		}

		public override Task FlushAsync (CancellationToken cancellationToken)
		{
			return Source.FlushAsync (cancellationToken);
		}

		public override void SetLength (long value)
		{
			throw new NotSupportedException ("The stream does not support resizing.");
		}
	}
}

View on GitHub (pinned to 9d3859a785)