jstedfast/MailKit · error · ArgumentException
The parent folder does not belong to this ImapClient.
Error message
The parent folder does not belong to this ImapClient.
What it means
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'.
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
Example fix
// before
// folder from old client, parent from new client
folder.Rename(newClient.GetFolder("Parent"), "Child"); // throws
// after
var sameClientParent = ((ImapClient)folder.FolderClient()).GetFolder("Parent");
folder.Rename(sameClientParent, "Child"); Defensive patterns
Strategy: validation
Validate before calling
bool SameConnection(IMailFolder f1, IMailFolder f2) =>
f1 is ImapFolder a && f2 is ImapFolder b && ReferenceEquals(a.Engine, b.Engine);
if (!SameConnection(folder, parent))
parent = ((ImapClient)folderSession).GetFolder(parent.FullName); Type guard
bool IsImapFolderFrom(IMailFolder f, ImapEngine engine) =>
f is ImapFolder imf && ReferenceEquals(imf.Engine, engine); Try / catch
try {
folder.Rename(parent, name);
} catch (ArgumentException ex) when (ex.ParamName == "parent") {
parent = currentClient.GetFolder(parent.FullName); // re-resolve on same client
folder.Rename(parent, name);
} Prevention
- Never cache IMailFolder references across ImapClient connections or reconnects
- Resolve all folder operations through a single connected ImapClient
- Re-fetch folders after Connect/Authenticate
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.
Related errors
- Cannot rename a folder using itself as the new parent…
- Cannot search for an empty set of unique identifiers.
- Cannot search for an invalid unique identifier.
- count
- Invalid unique identifier.
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/2290985bc7fb9cce.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/Net/Imap/ImapFolder.cs:1153
/// 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))
newFullName = parent.FullName + parent.DirectorySeparator + name;
else
newFullName = name;View on GitHub (pinned to 9d3859a785)