{"record":{"id":"a7b2b6db78e9d3bd","repo":"jstedfast/MailKit","slug":"one-or-more-of-the-messages-is-null","errorCode":null,"errorMessage":"One or more of the messages is null.","messagePattern":"One or more of the messages is null\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/IMailFolderAppendExtensions.cs","lineNumber":942,"sourceCode":"\t\t/// An I/O error occurred.\n\t\t/// </exception>\n\t\t/// <exception cref=\"ProtocolException\">\n\t\t/// The server's response contained unexpected tokens.\n\t\t/// </exception>\n\t\t/// <exception cref=\"CommandException\">\n\t\t/// The command failed.\n\t\t/// </exception>\n\t\tpublic static IList<UniqueId> Append (this IMailFolder folder, FormatOptions options, IList<MimeMessage> messages, IList<MessageFlags> flags, CancellationToken cancellationToken = default, ITransferProgress? progress = null)\n\t\t{\n\t\t\tif (options == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (options));\n\n\t\t\tif (messages == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (messages));\n\n\t\t\tfor (int i = 0; i < messages.Count; i++) {\n\t\t\t\tif (messages[i] == null)\n\t\t\t\t\tthrow new ArgumentException (\"One or more of the messages is null.\");\n\t\t\t}\n\n\t\t\tif (flags == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (flags));\n\n\t\t\tif (messages.Count != flags.Count)\n\t\t\t\tthrow new ArgumentException (\"The number of messages and the number of flags must be equal.\");\n\n\t\t\tvar requests = new AppendRequest[messages.Count];\n\t\t\tfor (int i = 0; i < messages.Count; i++) {\n\t\t\t\trequests[i] = new AppendRequest (messages[i], flags[i]) {\n\t\t\t\t\tTransferProgress = progress\n\t\t\t\t};\n\t\t\t}\n\n\t\t\treturn folder.Append (options, requests, cancellationToken);\n\t\t}\n","sourceCodeStart":924,"sourceCodeEnd":960,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/IMailFolderAppendExtensions.cs#L924-L960","documentation":"After confirming the list itself is non-null, Append scans every element and throws ArgumentException with message \"One or more of the messages is null.\" if any MimeMessage element is null. A partially-filled list would otherwise cause a null reference during serialization of the APPEND command.","triggerScenarios":"Calling folder.Append(options, new MimeMessage[]{ msg1, null, msg3 }, flags) — any single null element triggers this; also lists built by index assignment where some slots were never populated.","commonSituations":"Building batches in a loop where message construction failed for some items and null was appended as a placeholder; LINQ Select returning null for unmapped entries.","solutions":["Filter nulls before appending: messages = messages.Where(m => m != null).ToList() (adjust flags to match).","Throw or log at construction time when a message fails to build instead of storing null.","Assert batch contents before calling Append to catch the producer bug early."],"exampleFix":"// before\nfolder.Append(options, messages, flags);\n// after\nvar valid = messages.Where(m => m != null).ToList();\nvar validFlags = flags.Where((f, i) => messages[i] != null).ToList();\nfolder.Append(options, valid, validFlags);","handlingStrategy":"validation","validationCode":"if (messages.Any(m => m == null))\n    throw new InvalidOperationException(\"Batch contains null messages\");","typeGuard":"bool AllNonNull(IList<MimeMessage> list) => list.All(m => m != null);","tryCatchPattern":"try { folder.Append(options, messages, flags); } catch (ArgumentException ex) when (ex.Message.Contains(\"null\")) { /* rebuild batch without nulls */ }","preventionTips":["Never store null placeholders in message batches; throw at build time instead","Filter nulls before appending (keeping flags aligned)","Add batch-content assertions in tests"],"tags":["null-element","mailkit","csharp","argument-validation"],"backgroundTag":"null-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"}