{"record":{"id":"5e2c950879c6cc15","repo":"jstedfast/MailKit","slug":"the-destination-folder-does-not-belong-to-this-imapclient","errorCode":null,"errorMessage":"The destination folder does not belong to this ImapClient.","messagePattern":"The destination folder does not belong to this ImapClient\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolder.cs","lineNumber":190,"sourceCode":"\t\t\t\t\tthrow new FolderNotOpenException (FullName, access);\n\t\t\t}\n\t\t}\n\n\t\tvoid CheckAllowIndexes ()\n\t\t{\n\t\t\t// Indexes (\"Message Sequence Numbers\" or MSNs in the RFCs) and * are not stable while MessageNew/MessageExpunge is registered for SELECTED and therefore should not be used\n\t\t\t// https://tools.ietf.org/html/rfc5465#section-5.2\n\t\t\tif (Engine.NotifySelectedNewExpunge)\n\t\t\t\tthrow new InvalidOperationException (\"Indexes and '*' cannot be used while MessageNew/MessageExpunge is registered with NOTIFY for SELECTED.\");\n\t\t}\n\n\t\tvoid CheckValidDestination (IMailFolder destination)\n\t\t{\n\t\t\tif (destination == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (destination));\n\n\t\t\tif (destination is not ImapFolder target || (target.Engine != Engine))\n\t\t\t\tthrow new ArgumentException (\"The destination folder does not belong to this ImapClient.\", nameof (destination));\n\t\t}\n\n\t\tinternal void Reset ()\n\t\t{\n\t\t\t// basic state\n\t\t\t((HashSet<string>) PermanentKeywords).Clear ();\n\t\t\t((HashSet<string>) AcceptedKeywords).Clear ();\n\t\t\tPermanentFlags = MessageFlags.None;\n\t\t\tAcceptedFlags = MessageFlags.None;\n\t\t\tAccess = FolderAccess.None;\n\n\t\t\t// annotate state\n\t\t\tAnnotationAccess = AnnotationAccess.None;\n\t\t\tAnnotationScopes = AnnotationScope.None;\n\t\t\tMaxAnnotationSize = 0;\n\n\t\t\t// condstore state\n\t\t\tsupportsModSeq = false;","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolder.cs#L172-L208","documentation":"This ArgumentException comes from ImapFolder.CheckValidDestination when CopyTo or MoveTo receives a destination folder that is either not an ImapFolder or belongs to a different ImapClient instance (different Engine). An IMAP COPY/MOVE command can only target mailboxes on the same server session.","triggerScenarios":"Calling folder.CopyTo(destination) or folder.MoveTo(destination) where destination was obtained from another ImapClient (e.g. two connections to different or even the same server), or a non-ImapFolder IMailFolder implementation (local mail folder, MemoryFolder).","commonSituations":"Migrating mail between accounts by opening two ImapClients and moving messages between them; mixing folder objects from UnitTests or a different provider; copying a folder reference captured before the client was reconnected/recreated (new Engine instance).","solutions":["To move between servers/clients, copy by message content: GetMessage from source and AppendMessage to destination folder, then mark source as deleted and expunge.","Ensure destination is a folder from the SAME ImapClient instance (client.GetFolder(...) of the same connection).","If reconnecting, re-fetch folder references from the new ImapClient rather than reusing stale ones.","Check destination is ImapFolder and its Engine matches before calling CopyTo/MoveTo."],"exampleFix":"// before\nvar dest = otherClient.GetFolder(\"Archive\");\ninbox.MoveTo(dest);\n\n// after\nvar dest = client.GetFolder(\"Archive\");\ndest.Open(FolderAccess.ReadWrite);\ninbox.MoveTo(dest);\n// cross-client alternative:\n// dest.AppendMessage(inbox.GetMessage(uid));","handlingStrategy":"type-guard","validationCode":"if (destination is not ImapFolder t || !ReferenceEquals(t.Engine, ((ImapFolder)folder).Engine)) throw new InvalidOperationException(\"destination must belong to the same client\");","typeGuard":"bool IsSameClientDestination(IMailFolder src, IMailFolder dest) => src is ImapFolder s && dest is ImapFolder d && ReferenceEquals(s.Engine, d.Engine);","tryCatchPattern":"try { inbox.MoveTo(dest); } catch (ArgumentException) { MoveViaAppend(inbox, dest); }","preventionTips":["Only pass folders obtained from the same ImapClient instance to CopyTo/MoveTo.","Re-fetch folder references from the current client after any reconnect.","For cross-account/cross-server moves, use GetMessage + AppendMessage instead.","Avoid mixing IMailFolder implementations (local/maildir folders) with IMAP copies."],"tags":["imap","mailkit","argument-validation","copy-move"],"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-16T04:17:20.429Z"}