jstedfast/MailKit · error · InvalidOperationException
Indexes and '*' cannot be used while…
Error message
Indexes and '*' cannot be used while MessageNew/MessageExpunge is registered with NOTIFY for SELECTED.
What it means
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.
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.
Example fix
// before var summary = folder.GetMessage(0); // index with NOTIFY MessageNew/Expunge // after var uids = folder.Search(SearchQuery.All); var summary = folder.GetMessage(new UniqueId(uids[0].Id));
Defensive patterns
Strategy: validation
Validate before calling
if (!client.Capabilities.HasFlag(ImapCapabilities.Quota) /* NOTIFY check */) { /* use UID APIs or skip */ }
bool canUseIndexes = !EngineNotifySelectedNewExpunge(client); Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- Value cannot be null. (Parameter 'name')
- Value cannot be null. (Parameter 'rights')
- Value cannot be null. (Parameter 'array')
- Specified argument was out of range of valid values…
- Specified argument was out of range of valid values…
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/4da353fe8e06f8ea.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/Net/Imap/ImapFolder.cs:181
throw new ServiceNotConnectedException ("The ImapClient is not connected.");
if (Engine.State < ImapEngineState.Authenticated)
throw new ServiceNotAuthenticatedException ("The ImapClient is not authenticated.");
if (open) {
var access = rw ? FolderAccess.ReadWrite : FolderAccess.ReadOnly;
if (!IsOpen || Access < access)
throw new FolderNotOpenException (FullName, access);
}
}
void CheckAllowIndexes ()
{
// 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
// https://tools.ietf.org/html/rfc5465#section-5.2
if (Engine.NotifySelectedNewExpunge)
throw new InvalidOperationException ("Indexes and '*' cannot be used while MessageNew/MessageExpunge is registered with NOTIFY for SELECTED.");
}
void CheckValidDestination (IMailFolder destination)
{
if (destination == null)
throw new ArgumentNullException (nameof (destination));
if (destination is not ImapFolder target || (target.Engine != Engine))
throw new ArgumentException ("The destination folder does not belong to this ImapClient.", nameof (destination));
}
internal void Reset ()
{
// basic state
((HashSet<string>) PermanentKeywords).Clear ();
((HashSet<string>) AcceptedKeywords).Clear ();
PermanentFlags = MessageFlags.None;
AcceptedFlags = MessageFlags.None;View on GitHub (pinned to 9d3859a785)