jstedfast/MailKit · error · InvalidOperationException

Cannot rename this folder.

Error message

Cannot rename this folder.

What it means

Certain folders are structurally non-renamable: namespace root folders and the INBOX (IMAP forbids renaming INBOX). MailKit throws InvalidOperationException after detecting IsNamespace or the Inbox attribute.

Solutions

  1. Skip folders whose Attributes include FolderAttributes.Inbox and namespace roots when iterating
  2. Check folder.IsNamespace and the Inbox attribute before offering rename in UIs
  3. To 'rename' INBOX behavior, create a new folder and MOVE messages instead — IMAP prohibits renaming INBOX

Example fix

// before
foreach (var f in personalNamespace) f.Rename(f.ParentFolder, newName(f));
// after
foreach (var f in personalNamespace) {
    if (f.IsNamespace || (f.Attributes & FolderAttributes.Inbox) != 0) continue;
    f.Rename(f.ParentFolder, newName(f));
}
Defensive patterns

Strategy: validation

Validate before calling

bool CanRename(IMailFolder f) => !f.IsNamespace && (f.Attributes & FolderAttributes.Inbox) == 0;
if (CanRename(folder)) folder.Rename(folder.ParentFolder, newName);

Try / catch

try {
    folder.Rename(parent, name);
} catch (InvalidOperationException) {
    // folder is INBOX or a namespace root; skip or move contents instead
}

Prevention

When it happens

Trigger: Calling Rename on a folder where IsNamespace is true or (Attributes & FolderAttributes.Inbox) != 0 — typically renaming the INBOX itself or a personal-namespace root.

Common situations: Bulk rename scripts iterating all subfolders including the namespace root/INBOX; UIs listing INBOX alongside normal folders without marking it special.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

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

		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))
				newFullName = parent.FullName + parent.DirectorySeparator + name;
			else
				newFullName = name;

			encodedName = Engine.EncodeMailboxName (newFullName);

			return Engine.QueueCommand (cancellationToken, null, "RENAME %F %S\r\n", this, encodedName);
		}

		void ProcessRenameResponse (ImapCommand ic, IMailFolder parent, string name, string encodedName)
		{
			var oldFullName = FullName;

View on GitHub (pinned to 9d3859a785)