{"record":{"id":"b33e79aaa02468d6","repo":"jstedfast/MailKit","slug":"if-messagenew-or-messageexpunge-is-specified-both-must-be","errorCode":null,"errorMessage":"If MessageNew or MessageExpunge is specified, both must be specified.","messagePattern":"If MessageNew or MessageExpunge is specified, both must be specified\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapEventGroup.cs","lineNumber":158,"sourceCode":"\t\t\t\t\tif (@event is ImapEvent.MessageNew)\n\t\t\t\t\t\thaveMessageNew = true;\n\t\t\t\t\telse if (@event == ImapEvent.MessageExpunge)\n\t\t\t\t\t\thaveMessageExpunge = true;\n\t\t\t\t\telse if (@event == ImapEvent.FlagChange)\n\t\t\t\t\t\thaveFlagChange = true;\n\t\t\t\t\telse if (@event == ImapEvent.AnnotationChange)\n\t\t\t\t\t\thaveAnnotationChange = true;\n\n\t\t\t\t\tif (i > 0)\n\t\t\t\t\t\tcommand.Append (' ');\n\n\t\t\t\t\t@event.Format (engine, command, args, isSelectedFilter);\n\t\t\t\t}\n\t\t\t\tcommand.Append (')');\n\n\t\t\t\t// https://tools.ietf.org/html/rfc5465#section-5\n\t\t\t\tif ((haveMessageNew && !haveMessageExpunge) || (!haveMessageNew && haveMessageExpunge))\n\t\t\t\t\tthrow new InvalidOperationException (\"If MessageNew or MessageExpunge is specified, both must be specified.\");\n\n\t\t\t\tif ((haveFlagChange || haveAnnotationChange) && (!haveMessageNew || !haveMessageExpunge))\n\t\t\t\t\tthrow new InvalidOperationException (\"If FlagChange and/or AnnotationChange are specified, MessageNew and MessageExpunge must also be specified.\");\n\n\t\t\t\tnotifySelectedNewExpunge = (haveMessageNew || haveMessageExpunge) && MailboxFilter == ImapMailboxFilter.Selected;\n\t\t\t} else {\n\t\t\t\tcommand.Append (\"NONE\");\n\t\t\t}\n\n\t\t\tcommand.Append (')');\n\t\t}\n\t}\n\n\t/// <summary>\n\t/// An IMAP mailbox filter for use with the NOTIFY command.\n\t/// </summary>\n\t/// <remarks>\n\t/// An IMAP mailbox filter for use with the NOTIFY command.","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapEventGroup.cs#L140-L176","documentation":"Per RFC 5465 section 5, within a NOTIFY event group the MessageNew and MessageExpunge events are paired: if you specify one you must specify the other. ImapEventGroup.Format() enforces this when building the NOTIFY command and throws InvalidOperationException otherwise.","triggerScenarios":"Building an ImapEventGroup whose Events contain ImapEvent.MessageNew but not ImapEvent.MessageExpunge (or vice versa) with a non-empty event list, then issuing NOTIFY.","commonSituations":"Developers add MessageNew because they only care about new mail, unaware the IMAP NOTIFY spec requires the pair so the server can send consistent expunge information for QRESYNC/ESELECT.","solutions":["Add both ImapEvent.MessageNew and ImapEvent.MessageExpunge to the Events list","If you only want new-message notifications without expunges, still include MessageExpunge as required by the spec","Filter events out entirely (Events = empty -> NONE) if you don't need the pair"],"exampleFix":"// before\nvar group = new ImapEventGroup(ImapMailboxFilter.Selected) {\n    Events = { ImapEvent.MessageNew }\n};\n\n// after\nvar group = new ImapEventGroup(ImapMailboxFilter.Selected) {\n    Events = { ImapEvent.MessageNew, ImapEvent.MessageExpunge }\n};","handlingStrategy":"validation","validationCode":"bool HasPairedMessageEvents(ImapEventGroup g) =>\n    g.Events.Count == 0 ||\n    (g.Events.Contains(ImapEvent.MessageNew) == g.Events.Contains(ImapEvent.MessageExpunge));","typeGuard":"null","tryCatchPattern":"try {\n    client.Notify(groups);\n} catch (InvalidOperationException ex) when (ex.Message.Contains(\"MessageNew or MessageExpunge\")) {\n    // fix event list to include both\n}","preventionTips":["Always add MessageNew and MessageExpunge as a pair","Validate groups before issuing NOTIFY","Remember the rule comes from RFC 5465 section 5, not a MailKit quirk"],"tags":["imap","notify","rfc5465","validation"],"backgroundTag":"missing-required-argument","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}