jstedfast/MailKit · error · ArgumentException

Cannot rename a folder using itself as the new parent…

Error message

Cannot rename a folder using itself as the new parent folder.

What it means

QueueRenameCommand validates that the new parent folder for a rename is not the folder itself; renaming a folder under itself is meaningless in IMAP and would create a cycle. MailKit throws ArgumentException with paramName 'parent' before any network traffic.

Solutions

  1. Guard before calling: if (!ReferenceEquals(parent, folder)) then rename
  2. Choose the correct destination parent (usually folder.ParentFolder) instead of the folder itself
  3. Return a friendly validation message in UIs when the destination equals the source

Example fix

// before
folder.Rename(folder, "Renamed");
// after
if (!object.ReferenceEquals(folder, newParent))
    folder.Rename(newParent, "Renamed");
Defensive patterns

Strategy: validation

Validate before calling

if (ReferenceEquals(sourceFolder, destinationParent))
    throw new ArgumentException("Destination parent must differ from the folder being renamed.");
sourceFolder.Rename(destinationParent, newName);

Try / catch

try {
    folder.Rename(parent, name);
} catch (ArgumentException ex) when (ex.ParamName == "parent") {
    // surface: pick a different destination folder
}

Prevention

When it happens

Trigger: Calling folder.Rename(folder, "newname") — i.e. passing the same IMailFolder instance as both the folder being renamed and the destination parent (object.ReferenceEquals(parent, this)).

Common situations: Generic folder-management UIs where 'move to folder' lets the user pick the currently selected folder; code that computes the destination parent from the same variable as the source.

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/af435daaeec0a1b0. Report an issue: GitHub.

Appendix: source

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

		/// The server's response contained unexpected tokens.
		/// </exception>
		/// <exception cref="ImapCommandException">
		/// The server replied with a NO or BAD response.
		/// </exception>
		public override Task<IMailFolder?> CreateAsync (string name, IEnumerable<SpecialFolder> specialUses, CancellationToken cancellationToken = default)
		{
			var ic = QueueCreateCommand (name, specialUses, cancellationToken, out var encodedName);

			return CreateAsync (ic, encodedName, true, cancellationToken);
		}

		ImapCommand QueueRenameCommand (IMailFolder parent, string name, CancellationToken cancellationToken, out string encodedName)
		{
			if (parent == null)
				throw new ArgumentNullException (nameof (parent));

			if (object.ReferenceEquals (parent, this))
				throw new ArgumentException ("Cannot rename a folder using itself as the new parent folder.", nameof (parent));

			if (parent is not ImapFolder || ((ImapFolder) parent).Engine != Engine)
				throw new ArgumentException ("The parent folder does not belong to this ImapClient.", nameof (parent));

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

			if (!ImapEngine.IsValidMailboxName (name, DirectorySeparator))
				throw new ArgumentException ("The name is not a legal folder name.", nameof (name));

			if (IsNamespace || (Attributes & FolderAttributes.Inbox) != 0)
				throw new InvalidOperationException ("Cannot rename this folder.");

			CheckState (false, false);

			string newFullName;

			if (!string.IsNullOrEmpty (parent.FullName))

View on GitHub (pinned to 9d3859a785)