{"record":{"id":"94126d7ca6c55381","repo":"jstedfast/MailKit","slug":"the-destination-folder-does-not-belong-to-this-imapclient-94126d","errorCode":null,"errorMessage":"The destination folder does not belong to this ImapClient. (Parameter 'request')","messagePattern":"The destination folder does not belong to this ImapClient\\. \\(Parameter 'request'\\)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolder.cs","lineNumber":4864,"sourceCode":"\t\t\tif (uids == null)\n\t\t\t\treturn Array.Empty<UniqueId> ();\n\n\t\t\treturn uids;\n\t\t}\n\n\t\tvoid ValidateArguments (FormatOptions options, UniqueId uid, IReplaceRequest request)\n\t\t{\n\t\t\tif (options == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (options));\n\n\t\t\tif (!uid.IsValid)\n\t\t\t\tthrow new ArgumentException (\"The uid is invalid.\", nameof (uid));\n\n\t\t\tif (request == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (request));\n\n\t\t\tif (request.Destination != null && !(request.Destination is ImapFolder target && target.Engine == Engine))\n\t\t\t\tthrow new ArgumentException (\"The destination folder does not belong to this ImapClient.\", nameof (request));\n\n\t\t\tif (request.Annotations != null && request.Annotations.Count > 0 && (Engine.Capabilities & ImapCapabilities.Annotate) == 0)\n\t\t\t\tthrow new NotSupportedException (\"The IMAP server does not support annotations.\");\n\n\t\t\tCheckState (true, true);\n\t\t}\n\n\t\tImapCommand QueueReplaceCommand (FormatOptions options, UniqueId uid, IReplaceRequest request, CancellationToken cancellationToken)\n\t\t{\n\t\t\tvar format = CreateAppendOptions (options);\n\t\t\tint numKeywords = request.Keywords != null ? request.Keywords.Count : 0;\n\t\t\tvar builder = new StringBuilder ($\"UID REPLACE {uid} %F \");\n\t\t\tvar list = new List<object> {\n\t\t\t\trequest.Destination ?? this\n\t\t\t};\n\n\t\t\tif ((request.Flags & SettableFlags) != 0 || numKeywords > 0) {\n\t\t\t\tImapUtils.FormatFlagsList (builder, request.Flags, numKeywords);","sourceCodeStart":4846,"sourceCodeEnd":4882,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolder.cs#L4846-L4882","documentation":"When a replace request specifies a Destination folder, MailKit verifies that destination is an ImapFolder belonging to the same ImapClient (same Engine) as the source folder. Otherwise it throws ArgumentException, because IMAP cannot MOVE/REPLACE across different connections.","triggerScenarios":"Calling Replace with request.Destination set to an ImapFolder opened on a different ImapClient, or to a non-ImapFolder IMailFolder implementation (e.g. a wrapper or a memory folder).","commonSituations":"Two IMAP connections (e.g. one per account or one per thread) and folders passed between them; custom IMailFolder decorators; unit tests using fake folders.","solutions":["Use a destination folder obtained from the same ImapClient as the source folder","Open the destination on the same connection: client.GetFolder(destPath) from the same client instance","If cross-account copy is needed, download the message and append it via the other client","Wrap in try/catch (ArgumentException) and fall back to fetch+append"],"exampleFix":"// before\nvar otherClient = new ImapClient(); // different connection\nrequest.Destination = (ImapFolder)otherClient.GetFolder(\"Archive\");\nfolder.Replace(FormatOptions.Default, uid, request);\n// after\nrequest.Destination = (ImapFolder)client.GetFolder(\"Archive\"); // same client as 'folder'\nfolder.Replace(FormatOptions.Default, uid, request);","handlingStrategy":"type-guard","validationCode":"if (request.Destination is ImapFolder dest && dest.Engine != ((ImapFolder)folder).Engine)\n    throw new InvalidOperationException(\"destination belongs to a different ImapClient\");","typeGuard":"bool SameClient(ImapFolder src, IMailFolder dest) => dest is ImapFolder d && d.Engine == src.Engine;","tryCatchPattern":"try { folder.Replace(FormatOptions.Default, uid, request); }\ncatch (ArgumentException) { // fallback: fetch + append via the other client\n    var msg = folder.GetMessage(uid); otherFolder.Append(msg); folder.AddFlags(uid, MessageFlags.Deleted, true); }","preventionTips":["Keep one ImapClient per account and never mix folders across clients","Resolve destination folders via the same client instance","Avoid custom IMailFolder wrappers as replace destinations"],"tags":["imap","destination-folder","cross-client","argument-validation"],"backgroundTag":"invalid-argument-value","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"}