jstedfast/MailKit · error · ArgumentException
The destination folder does not belong to this ImapClient…
Error message
The destination folder does not belong to this ImapClient. (Parameter 'request')
What it means
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.
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
Example fix
// before
var otherClient = new ImapClient(); // different connection
request.Destination = (ImapFolder)otherClient.GetFolder("Archive");
folder.Replace(FormatOptions.Default, uid, request);
// after
request.Destination = (ImapFolder)client.GetFolder("Archive"); // same client as 'folder'
folder.Replace(FormatOptions.Default, uid, request); Defensive patterns
Strategy: type-guard
Validate before calling
if (request.Destination is ImapFolder dest && dest.Engine != ((ImapFolder)folder).Engine)
throw new InvalidOperationException("destination belongs to a different ImapClient"); Type guard
bool SameClient(ImapFolder src, IMailFolder dest) => dest is ImapFolder d && d.Engine == src.Engine;
Try / catch
try { folder.Replace(FormatOptions.Default, uid, request); }
catch (ArgumentException) { // fallback: fetch + append via the other client
var msg = folder.GetMessage(uid); otherFolder.Append(msg); folder.AddFlags(uid, MessageFlags.Deleted, true); } Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- The parent folder does not belong to this ImapClient.
- All folders must be ImapFolders.
- Annotation entry paths must not end with '/'.
- Annotation entry paths must not end with '.'.
- Cannot rename a folder using itself as the new parent…
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/94126d7ca6c55381.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/Net/Imap/ImapFolder.cs:4864
if (uids == null)
return Array.Empty<UniqueId> ();
return uids;
}
void ValidateArguments (FormatOptions options, UniqueId uid, IReplaceRequest request)
{
if (options == null)
throw new ArgumentNullException (nameof (options));
if (!uid.IsValid)
throw new ArgumentException ("The uid is invalid.", nameof (uid));
if (request == null)
throw new ArgumentNullException (nameof (request));
if (request.Destination != null && !(request.Destination is ImapFolder target && target.Engine == Engine))
throw new ArgumentException ("The destination folder does not belong to this ImapClient.", nameof (request));
if (request.Annotations != null && request.Annotations.Count > 0 && (Engine.Capabilities & ImapCapabilities.Annotate) == 0)
throw new NotSupportedException ("The IMAP server does not support annotations.");
CheckState (true, true);
}
ImapCommand QueueReplaceCommand (FormatOptions options, UniqueId uid, IReplaceRequest request, CancellationToken cancellationToken)
{
var format = CreateAppendOptions (options);
int numKeywords = request.Keywords != null ? request.Keywords.Count : 0;
var builder = new StringBuilder ($"UID REPLACE {uid} %F ");
var list = new List<object> {
request.Destination ?? this
};
if ((request.Flags & SettableFlags) != 0 || numKeywords > 0) {
ImapUtils.FormatFlagsList (builder, request.Flags, numKeywords);View on GitHub (pinned to 9d3859a785)