jstedfast/MailKit · error · InvalidOperationException
The MessageNew event cannot have any parameters for mailbox…
Error message
The MessageNew event cannot have any parameters for mailbox filters other than SELECTED and SELECTED-DELAYED.
What it means
The MessageNew event can carry a fetch request (summary items) only when the event group's mailbox filter is SELECTED or SELECTED-DELAYED; for other filters (Personal, Subscribed, mailboxes list) RFC 5465 forbids parameters on MessageNew. Format() throws InvalidOperationException if a request is attached and isSelectedFilter is false.
Solutions
- Remove the parameters so MessageNew is parameterless for non-SELECTED filters (new ImapEvent.MessageNew() or ImapEvent.MessageNew as appropriate)
- Use MailboxFilter.Selected/SelectedDelayed if you genuinely need MessageNew with summary items and only care about the currently selected mailbox
- Fetch full summaries yourself in the notification handler after receiving the event
Example fix
// before
var group = new ImapEventGroup(ImapMailboxFilter.Personal) {
Events = { new ImapEvent.MessageNew(MessageSummaryItems.UniqueId | MessageSummaryItems.Envelope) }
};
// after
var group = new ImapEventGroup(ImapMailboxFilter.Personal) {
Events = { ImapEvent.MessageNew, ImapEvent.MessageExpunge }
}; Defensive patterns
Strategy: validation
Validate before calling
bool MessageNewParamsAllowed(ImapEventGroup g) =>
g.MailboxFilter is ImapMailboxFilter.Selected or ImapMailboxFilter.SelectedDelayed
|| g.Events.All(e => e is not ImapEvent.MessageNew mn || ImapFolder.IsEmptyFetchRequest(mn.Request)); Type guard
null
Try / catch
try {
client.Notify(groups);
} catch (InvalidOperationException ex) when (ex.Message.Contains("MessageNew event cannot have any parameters")) {
// rebuild with parameterless MessageNew for non-selected filters
} Prevention
- Only attach summary-item requests to MessageNew in SELECTED/SELECTED-DELAYED groups
- Check the filter type before configuring MessageNew parameters
- Handle new-message data needs by fetching in the event handler instead
When it happens
Trigger: Creating an ImapEventGroup with MailboxFilter = Personal (or Subscribed/Mailboxes) and setting the MessageNew event's request items (e.g. via new ImapEvent.MessageNew(summaryItems)), then issuing NOTIFY.
Common situations: Copy-pasting a SELECTED-group MessageNew configuration into a Personal group; wanting preview/summary data for new messages across all personal folders, which NOTIFY does not allow.
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
- Only message events may be specified when SELECTED or…
- If MessageNew or MessageExpunge is specified, both must be…
- If FlagChange and/or AnnotationChange are specified…
- No event groups specified.
- The IMAP server does not support the NOTIFY extension.
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/bb34c8cf0135c704.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/Net/Imap/ImapEventGroup.cs:725
/// </summary>
/// <remarks>
/// Formats the IMAP NOTIFY command for this particular IMAP mailbox filter.
/// </remarks>
/// <param name="engine">The IMAP engine.</param>
/// <param name="command">The IMAP command builder.</param>
/// <param name="args">The IMAP command argument builder.</param>
/// <param name="isSelectedFilter"><see langword="true" /> if the event is being registered for a
/// <see cref="ImapMailboxFilter.Selected"/> or <see cref="ImapMailboxFilter.SelectedDelayed"/>
/// mailbox filter.</param>
internal override void Format (ImapEngine engine, StringBuilder command, IList<object> args, bool isSelectedFilter)
{
command.Append (Name);
if (ImapFolder.IsEmptyFetchRequest (request))
return;
if (!isSelectedFilter)
throw new InvalidOperationException ("The MessageNew event cannot have any parameters for mailbox filters other than SELECTED and SELECTED-DELAYED.");
command.Append (' ');
command.Append (ImapFolder.FormatSummaryItems (engine, request, out _, isNotify: true));
}
}
}
}
View on GitHub (pinned to 9d3859a785)