{"record":{"id":"7074619c978be96b","repo":"jstedfast/MailKit","slug":"value-cannot-be-null-parameter-message-messagesenteventargs","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'message')","messagePattern":"Value cannot be null\\. \\(Parameter 'message'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MailKit/MessageSentEventArgs.cs","lineNumber":56,"sourceCode":"\tpublic class MessageSentEventArgs : EventArgs\n\t{\n\t\t/// <summary>\n\t\t/// Initializes a new instance of the <see cref=\"MailKit.MessageSentEventArgs\"/> class.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Creates a new <see cref=\"MessageSentEventArgs\"/>.\n\t\t/// </remarks>\n\t\t/// <param name=\"message\">The message that was just sent.</param>\n\t\t/// <param name=\"response\">The response from the server.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <para><paramref name=\"message\"/> is <see langword=\"null\" />.</para>\n\t\t/// <para>-or-</para>\n\t\t/// <para><paramref name=\"response\"/> is <see langword=\"null\" />.</para>\n\t\t/// </exception>\n\t\tpublic MessageSentEventArgs (MimeMessage message, string response)\n\t\t{\n\t\t\tif (message == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (message));\n\n\t\t\tif (response == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (response));\n\n\t\t\tMessage = message;\n\t\t\tResponse = response;\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Get the message that was just sent.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Gets the message that was just sent.\n\t\t/// </remarks>\n\t\t/// <value>The message.</value>\n\t\tpublic MimeMessage Message {\n\t\t\tget; private set;\n\t\t}","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/MessageSentEventArgs.cs#L38-L74","documentation":"MessageSentEventArgs' constructor throws ArgumentNullException when the MimeMessage passed as `message` is null. MailKit will only create a MessageSentEventArgs for an actual sent message, guaranteeing the Message property is non-null for event handlers.","triggerScenarios":"Calling `new MessageSentEventArgs(null, response)`, or raising a MessageSent event after a Send call returned null because the message failed to serialize or the caller passed a null message to Send.","commonSituations":"Custom SmtpClient/transport implementations that raise MessageSent with a null message when delivery fails silently; logging wrappers that record the event with whatever object they hold, which is null on failure paths; tests that forget to create the MimeMessage.","solutions":["Ensure the MimeMessage is fully constructed before sending / raising the event; never call Send(null, ...).","Guard before constructing: `if (message == null) return;` or throw your own descriptive error.","If the message is null because delivery failed, handle the send failure path instead of emitting a MessageSent event."],"exampleFix":"// before\nsmtp.MessageSent += (s, e) => { }; // event raised with null message internally\nsmtp.Send(null, \"250 OK\");\n// after\nvar message = MimeMessage.CreateFromMailMessage(mailMessage);\nif (message != null)\n    smtp.Send(message, \"250 OK\");","handlingStrategy":"validation","validationCode":"if (message == null)\n    throw new InvalidOperationException(\"Cannot send: MimeMessage is null.\");\nvar args = new MessageSentEventArgs(message, response);","typeGuard":"bool IsValidMessage(MimeMessage message) => message != null;","tryCatchPattern":"try {\n    var args = new MessageSentEventArgs(message, response);\n} catch (ArgumentNullException ex) when (ex.ParamName == \"message\") {\n    // log and skip: nothing was actually sent\n    logger.LogWarning(\"MessageSent event skipped: message was null\");\n}","preventionTips":["Check for null before calling SmtpClient.Send; construct the MimeMessage in one dedicated step.","Don't raise MessageSent events on failed-delivery paths.","Assert non-null message in tests that simulate SMTP sends."],"tags":["csharp","mailkit","argument-null","smtp"],"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"}