{"record":{"id":"386b6690367c971e","repo":"jstedfast/MailKit","slug":"one-or-more-of-the-headers-is-invalid","errorCode":null,"errorMessage":"One or more of the headers is invalid.","messagePattern":"One or more of the headers is invalid\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/HeaderSet.cs","lineNumber":280,"sourceCode":"\t\t/// <exception cref=\"ArgumentNullException\">\n\t\t/// <paramref name=\"headers\"/> is <see langword=\"null\" />.\n\t\t/// </exception>\n\t\t/// <exception cref=\"ArgumentException\">\n\t\t/// One or more of the specified <paramref name=\"headers\"/> is invalid.\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 void AddRange (IEnumerable<HeaderId> headers)\n\t\t{\n\t\t\tif (headers == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (headers));\n\n\t\t\tCheckReadOnly ();\n\n\t\t\tforeach (var header in headers) {\n\t\t\t\tif (header == HeaderId.Unknown)\n\t\t\t\t\tthrow new ArgumentException (\"One or more of the headers is invalid.\", nameof (headers));\n\n\t\t\t\thash.Add (header.ToHeaderName ().ToUpperInvariant ());\n\t\t\t}\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Add a collection of headers.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Adds the specified headers to the set of headers.\n\t\t/// </remarks>\n\t\t/// <param name=\"headers\">The headers to add.</param>\n\t\t/// <exception cref=\"ArgumentNullException\">\n\t\t/// <paramref name=\"headers\"/> is <see langword=\"null\" />.\n\t\t/// </exception>\n\t\t/// <exception cref=\"ArgumentException\">\n\t\t/// One or more of the specified <paramref name=\"headers\"/> is invalid.\n\t\t/// </exception>","sourceCodeStart":262,"sourceCodeEnd":298,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/HeaderSet.cs#L262-L298","documentation":"HeaderSet.AddRange(IEnumerable<HeaderId>) throws ArgumentException(\"One or more of the headers is invalid.\") when any element in the collection equals HeaderId.Unknown. Unknown is a placeholder, not a real header.","triggerScenarios":"Passing collections containing HeaderId.Unknown, typically produced by HeaderId.FromHeaderName on unrecognized names.","commonSituations":"Bulk-adding parsed header names where some didn't map to known HeaderId values; default enum values in structs/lists.","solutions":["Filter out HeaderId.Unknown before AddRange: headers.Where (h => h != HeaderId.Unknown)","Fall back to the string-based AddRange for names that don't map to a HeaderId","Validate the collection elements before the call"],"exampleFix":"// before\nset.AddRange (parsedIds); // may contain HeaderId.Unknown\n// after\nset.AddRange (parsedIds.Where (id => id != HeaderId.Unknown));","handlingStrategy":"validation","validationCode":"var valid = headers.Where (h => h != HeaderId.Unknown);\nset.AddRange (valid);","typeGuard":"static bool IsUsableHeader (HeaderId id) => id != HeaderId.Unknown;","tryCatchPattern":"try {\n    set.AddRange (headers);\n} catch (ArgumentException ex) {\n    set.AddRange (headers.Where (h => h != HeaderId.Unknown));\n}","preventionTips":["Map names to HeaderId and filter Unknown results before bulk adds","Keep unknown names in a separate string-based set","Validate enum lists after parsing, before AddRange"],"tags":["csharp","mailkit","argument","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"}