{"record":{"id":"2290985bc7fb9cce","repo":"jstedfast/MailKit","slug":"the-parent-folder-does-not-belong-to-this-imapclient","errorCode":null,"errorMessage":"The parent folder does not belong to this ImapClient.","messagePattern":"The parent folder does not belong to this ImapClient\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolder.cs","lineNumber":1153,"sourceCode":"\t\t/// The server replied with a NO or BAD response.\n\t\t/// </exception>\n\t\tpublic override Task<IMailFolder?> CreateAsync (string name, IEnumerable<SpecialFolder> specialUses, CancellationToken cancellationToken = default)\n\t\t{\n\t\t\tvar ic = QueueCreateCommand (name, specialUses, cancellationToken, out var encodedName);\n\n\t\t\treturn CreateAsync (ic, encodedName, true, cancellationToken);\n\t\t}\n\n\t\tImapCommand QueueRenameCommand (IMailFolder parent, string name, CancellationToken cancellationToken, out string encodedName)\n\t\t{\n\t\t\tif (parent == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (parent));\n\n\t\t\tif (object.ReferenceEquals (parent, this))\n\t\t\t\tthrow new ArgumentException (\"Cannot rename a folder using itself as the new parent folder.\", nameof (parent));\n\n\t\t\tif (parent is not ImapFolder || ((ImapFolder) parent).Engine != Engine)\n\t\t\t\tthrow new ArgumentException (\"The parent folder does not belong to this ImapClient.\", nameof (parent));\n\n\t\t\tif (name == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (name));\n\n\t\t\tif (!ImapEngine.IsValidMailboxName (name, DirectorySeparator))\n\t\t\t\tthrow new ArgumentException (\"The name is not a legal folder name.\", nameof (name));\n\n\t\t\tif (IsNamespace || (Attributes & FolderAttributes.Inbox) != 0)\n\t\t\t\tthrow new InvalidOperationException (\"Cannot rename this folder.\");\n\n\t\t\tCheckState (false, false);\n\n\t\t\tstring newFullName;\n\n\t\t\tif (!string.IsNullOrEmpty (parent.FullName))\n\t\t\t\tnewFullName = parent.FullName + parent.DirectorySeparator + name;\n\t\t\telse\n\t\t\t\tnewFullName = name;","sourceCodeStart":1135,"sourceCodeEnd":1171,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolder.cs#L1135-L1171","documentation":"The rename's destination parent must be an ImapFolder created by the same ImapClient (sharing the same ImapEngine). Passing null-check-passing but foreign parent — another client's folder, a mock, or a non-IMAP IMailFolder — throws ArgumentException with paramName 'parent'.","triggerScenarios":"Calling folder.Rename(parent, name) where parent is not an ImapFolder, or is an ImapFolder whose Engine differs from this folder's Engine — e.g. folders obtained from two different ImapClient connections.","commonSituations":"Holding IMailFolder references across reconnects (stale folder from a disposed client used with a new one); mixing folders from a cache populated by a different account/session.","solutions":["Re-fetch the parent from the same ImapClient (client.GetFolder(parentFullName)) before renaming","Ensure both folders come from one connected ImapClient instance; reconnect and re-resolve folders after disconnects","Verify the object is actually an ImapFolder from the same connection, not a MemoryMailFolder or another provider"],"exampleFix":"// before\n// folder from old client, parent from new client\nfolder.Rename(newClient.GetFolder(\"Parent\"), \"Child\"); // throws\n// after\nvar sameClientParent = ((ImapClient)folder.FolderClient()).GetFolder(\"Parent\");\nfolder.Rename(sameClientParent, \"Child\");","handlingStrategy":"validation","validationCode":"bool SameConnection(IMailFolder f1, IMailFolder f2) =>\n    f1 is ImapFolder a && f2 is ImapFolder b && ReferenceEquals(a.Engine, b.Engine);\nif (!SameConnection(folder, parent))\n    parent = ((ImapClient)folderSession).GetFolder(parent.FullName);","typeGuard":"bool IsImapFolderFrom(IMailFolder f, ImapEngine engine) =>\n    f is ImapFolder imf && ReferenceEquals(imf.Engine, engine);","tryCatchPattern":"try {\n    folder.Rename(parent, name);\n} catch (ArgumentException ex) when (ex.ParamName == \"parent\") {\n    parent = currentClient.GetFolder(parent.FullName); // re-resolve on same client\n    folder.Rename(parent, name);\n}","preventionTips":["Never cache IMailFolder references across ImapClient connections or reconnects","Resolve all folder operations through a single connected ImapClient","Re-fetch folders after Connect/Authenticate"],"tags":["imap","mailkit","argument-validation","cross-client"],"backgroundTag":"incompatible-source-type","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}