{"record":{"id":"4da353fe8e06f8ea","repo":"jstedfast/MailKit","slug":"indexes-and-cannot-be-used-while-messagenew-messageexpunge","errorCode":null,"errorMessage":"Indexes and '*' cannot be used while MessageNew/MessageExpunge is registered with NOTIFY for SELECTED.","messagePattern":"Indexes and '\\*' cannot be used while MessageNew/MessageExpunge is registered with NOTIFY for SELECTED\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolder.cs","lineNumber":181,"sourceCode":"\t\t\t\tthrow new ServiceNotConnectedException (\"The ImapClient is not connected.\");\n\n\t\t\tif (Engine.State < ImapEngineState.Authenticated)\n\t\t\t\tthrow new ServiceNotAuthenticatedException (\"The ImapClient is not authenticated.\");\n\n\t\t\tif (open) {\n\t\t\t\tvar access = rw ? FolderAccess.ReadWrite : FolderAccess.ReadOnly;\n\n\t\t\t\tif (!IsOpen || Access < access)\n\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;","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolder.cs#L163-L199","documentation":"This InvalidOperationException is thrown by ImapFolder.CheckAllowIndexes when a command uses message sequence numbers (indexes) or '*' while the NOTIFY extension has MessageNew/MessageExpunge registered for the SELECTED state. Under RFC 5465, such events make MSNs unstable, so MailKit refuses the operation to avoid acting on stale indexes.","triggerScenarios":"Calling GetMessages(index-based ranges), GetMessage by index, CopyTo/MoveTo with indexes, Fetch by index range, or Search queries using '*' after enabling NOTIFY with MessageNew/MessageExpunge for SELECTED (Engine.NotifySelectedNewExpunge true, e.g. via ImapClient.NotifyFlags/NotifyCommands).","commonSituations":"Apps that enable CONDSTORE/QRESYNC-style change notification (NotifyFlags with MessageNew|MessageExpunge) and then keep using index-based APIs; mixed legacy code using GetMessages(0, -1) with push notifications enabled.","solutions":["Switch to UID-based APIs: GetMessageUids/GetMessages(uid range), GetUidNext/GetUidValidity, or ImapFolder.Fetch by UID.","Reconfigure NOTIFY to not register MessageNew/MessageExpunge for SELECTED (adjust ImapClient.NotifyFlags), disabling this restriction.","Convert existing sequence-number logic to use UniqueId ranges obtained via GetUids/Search.","Use Search queries returning UniqueIds instead of '*' index expressions."],"exampleFix":"// before\nvar summary = folder.GetMessage(0); // index with NOTIFY MessageNew/Expunge\n\n// after\nvar uids = folder.Search(SearchQuery.All);\nvar summary = folder.GetMessage(new UniqueId(uids[0].Id));","handlingStrategy":"validation","validationCode":"if (!client.Capabilities.HasFlag(ImapCapabilities.Quota) /* NOTIFY check */) { /* use UID APIs or skip */ }\nbool canUseIndexes = !EngineNotifySelectedNewExpunge(client);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer UID-based APIs (GetMessage by UniqueId, Search returning UniqueIds) throughout the app.","Do not enable MessageNew/MessageExpunge in NOTIFY for SELECTED unless you only use UID APIs.","Centralize NOTIFY configuration so index APIs and notification flags are consistent.","Wrap index-based calls in try/catch for InvalidOperationException if NOTIFY may be active."],"tags":["imap","mailkit","notify-extension","sequence-numbers"],"backgroundTag":"unsupported-operation","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}