{"record":{"id":"6651e88b85aa2291","repo":"jstedfast/MailKit","slug":"argumentoutofrangeexception-headerset","errorCode":null,"errorMessage":"ArgumentOutOfRangeException","messagePattern":"ArgumentOutOfRangeException","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MailKit/HeaderSet.cs","lineNumber":203,"sourceCode":"\n\t\t/// <summary>\n\t\t/// Add the specified header.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Adds the specified header to the set of headers.\n\t\t/// </remarks>\n\t\t/// <returns><see langword=\"true\" /> if the header was added to the set; otherwise, <see langword=\"false\" />.</returns>\n\t\t/// <param name=\"header\">The header to add.</param>\n\t\t/// <exception cref=\"ArgumentOutOfRangeException\">\n\t\t/// <paramref name=\"header\"/> is not a valid <see cref=\"HeaderId\"/>.\n\t\t/// </exception>\n\t\t/// <exception cref=\"InvalidOperationException\">\n\t\t/// The operation is invalid because the <see cref=\"HeaderSet\"/> is read-only.\n\t\t/// </exception>\n\t\tpublic bool Add (HeaderId header)\n\t\t{\n\t\t\tif (header == HeaderId.Unknown)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (header));\n\n\t\t\tCheckReadOnly ();\n\n\t\t\treturn hash.Add (header.ToHeaderName ().ToUpperInvariant ());\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Add the specified header.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Adds the specified header to the set of headers.\n\t\t/// </remarks>\n\t\t/// <returns><see langword=\"true\" /> if the header was added to the set; otherwise, <see langword=\"false\" />.</returns>\n\t\t/// <param name=\"header\">The header to add.</param>\n\t\t/// <exception cref=\"ArgumentNullException\">\n\t\t/// <paramref name=\"header\"/> is <see langword=\"null\" />.\n\t\t/// </exception>\n\t\t/// <exception cref=\"InvalidOperationException\">","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/HeaderSet.cs#L185-L221","documentation":"HeaderSet.Add(HeaderId) throws ArgumentOutOfRangeException when the header is HeaderId.Unknown, because Unknown is a sentinel, not a real header, and cannot be part of a header set. It also throws InvalidOperationException if the set is read-only.","triggerScenarios":"Passing HeaderId.Unknown to Add(HeaderId); commonly the result of HeaderId.FromHeaderName on an unrecognized name.","commonSituations":"Parsing header names from config or user input where the name lookup falls back to Unknown; default-initialized enum variables; mapping untrusted strings to HeaderId.","solutions":["Verify the header resolves to a real HeaderId (not Unknown) before calling Add","Parse with HeaderId.FromHeaderName and check the result, adding the raw string overload Add(string) instead if the name is custom","Ensure enum variables are initialized to an actual HeaderId"],"exampleFix":"// before\nvar id = HeaderId.FromHeaderName (name);\nset.Add (id); // throws if name unknown\n// after\nvar id = HeaderId.FromHeaderName (name);\nif (id == HeaderId.Unknown)\n    set.Add (name); // keep custom header as string\nelse\n    set.Add (id);","handlingStrategy":"validation","validationCode":"if (header == HeaderId.Unknown)\n    throw new ArgumentException (\"Header must be a known HeaderId\", nameof (header));","typeGuard":"static bool IsKnownHeader (HeaderId id) => id != HeaderId.Unknown;","tryCatchPattern":"try {\n    set.Add (header);\n} catch (ArgumentOutOfRangeException) {\n    set.Add (rawHeaderName); // fall back to string form\n}","preventionTips":["After HeaderId.FromHeaderName, always check for Unknown","Prefer the string Add overload for custom/uncommon headers","Initialize HeaderId variables to a real value, not default"],"tags":["csharp","mailkit","argument-out-of-range","enum"],"backgroundTag":"invalid-enum-value","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"}