{"record":{"id":"f4b67988f58ac2f0","repo":"jstedfast/MailKit","slug":"all-folders-must-be-imapfolders","errorCode":null,"errorMessage":"All folders must be ImapFolders.","messagePattern":"All folders must be ImapFolders\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapEventGroup.cs","lineNumber":314,"sourceCode":"\t\t\t/// </exception>\n\t\t\t/// <exception cref=\"System.ArgumentException\">\n\t\t\t/// <para>The list of <paramref name=\"folders\"/> is empty.</para>\n\t\t\t/// <para>-or-</para>\n\t\t\t/// <para>The list of <paramref name=\"folders\"/> contains folders that are not of\n\t\t\t/// type <see cref=\"ImapFolder\"/>.</para>\n\t\t\t/// </exception>\n\t\t\tinternal Mailboxes (string name, IList<IMailFolder> folders) : base (name)\n\t\t\t{\n\t\t\t\tif (folders == null)\n\t\t\t\t\tthrow new ArgumentNullException (nameof (folders));\n\n\t\t\t\tif (folders.Count == 0)\n\t\t\t\t\tthrow new ArgumentException (\"Must supply at least one folder.\", nameof (folders));\n\n\t\t\t\tthis.folders = new ImapFolder[folders.Count];\n\t\t\t\tfor (int i = 0; i < folders.Count; i++) {\n\t\t\t\t\tif (folders[i] is not ImapFolder folder)\n\t\t\t\t\t\tthrow new ArgumentException (\"All folders must be ImapFolders.\", nameof (folders));\n\n\t\t\t\t\tthis.folders[i] = folder;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t/// <summary>\n\t\t\t/// Format the IMAP NOTIFY command for this particular IMAP mailbox filter.\n\t\t\t/// </summary>\n\t\t\t/// <remarks>\n\t\t\t/// Formats the IMAP NOTIFY command for this particular IMAP mailbox filter.\n\t\t\t/// </remarks>\n\t\t\t/// <param name=\"engine\">The IMAP engine.</param>\n\t\t\t/// <param name=\"command\">The IMAP command builder.</param>\n\t\t\t/// <param name=\"args\">The IMAP command argument builder.</param>\n\t\t\tinternal override void Format (ImapEngine engine, StringBuilder command, IList<object> args)\n\t\t\t{\n\t\t\t\tcommand.Append (Name);\n\t\t\t\tcommand.Append (' ');","sourceCodeStart":296,"sourceCodeEnd":332,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapEventGroup.cs#L296-L332","documentation":"The Mailboxes event group requires every folder in the list to be an ImapFolder (the concrete MailKit implementation), because it stores them as ImapFolder[] to format IMAP mailbox names. Passing any other IMailFolder implementation throws ArgumentException(\"All folders must be ImapFolders.\").","triggerScenarios":"Calling ImapEventGroup.Mailboxes with a list containing IMailFolder instances that are not ImapFolder - e.g. wrapper/decorator implementations, mocks from a testing library, or folders from a different provider (IMailFolder fakes).","commonSituations":"Unit tests passing mock IMailFolder objects; custom adapter classes implementing IMailFolder around another store; mixing folders obtained from a non-ImapClient source.","solutions":["Use only folders returned by ImapClient/ImapFolder APIs (GetFolder, GetSubfolders, Inbox) so they are real ImapFolder instances","Unwrap decorator/wrapper IMailFolder implementations to the underlying ImapFolder before building the group","In tests, use real ImapFolder objects or a fake IMAP server rather than interface mocks"],"exampleFix":"// before\nIList<IMailFolder> folders = new List<IMailFolder> { new MyFolderWrapper(realFolder) };\nvar group = ImapEventGroup.Mailboxes(\"notify\", folders);\n\n// after\nvar folders = matchedFolders.OfType<ImapFolder>().ToList();\nvar group = ImapEventGroup.Mailboxes(\"notify\", folders);","handlingStrategy":"type-guard","validationCode":"var imapFolders = folders.OfType<ImapFolder>().ToList();\nif (imapFolders.Count != folders.Count)\n    throw new ArgumentException(\"List contains non-ImapFolder IMailFolder implementations.\");","typeGuard":"bool AllAreImapFolders(IList<IMailFolder> folders) => folders.All(f => f is ImapFolder);","tryCatchPattern":"try {\n    var group = ImapEventGroup.Mailboxes(name, folders);\n} catch (ArgumentException ex) when (ex.Message.Contains(\"ImapFolders\")) {\n    // filter with OfType<ImapFolder>() and retry\n}","preventionTips":["Only feed folders obtained from ImapClient into MailKit APIs","Avoid mocking IMailFolder in tests that exercise MailKit internals","Unwrap custom wrappers before passing folder lists"],"tags":["imap","notify","type-mismatch","argument-validation"],"backgroundTag":"type-mismatch","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"}