jstedfast/MailKit · error · ArgumentNullException

Value cannot be null. (Parameter 'response')

Error message

Value cannot be null. (Parameter 'response')

What it means

MessageSentEventArgs' constructor throws ArgumentNullException when the server `response` string is null. MailKit records the SMTP server's reply alongside the sent message, and requires a non-null (possibly empty) response string for event handlers.

Solutions

  1. Pass the actual server response string, or an empty string "" if none is available: `new MessageSentEventArgs(message, response ?? "")`.
  2. In custom SmtpClient implementations, ensure the final server reply is read before raising MessageSent.
  3. In mocks, return a valid response line such as "250 OK" instead of null.

Example fix

// before
var args = new MessageSentEventArgs(message, response);
// after
var args = new MessageSentEventArgs(message, response ?? string.Empty);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(response))
    response = string.Empty;
var args = new MessageSentEventArgs(message, response);

Type guard

bool IsValidResponse(string response) => response != null;

Try / catch

try {
    var args = new MessageSentEventArgs(message, response);
} catch (ArgumentNullException ex) when (ex.ParamName == "response") {
    var args = new MessageSentEventArgs(message, string.Empty);
}

Prevention

When it happens

Trigger: Calling `new MessageSentEventArgs(message, null)`, or a custom SmtpClient implementation that raises MessageSent with a null response because it never read the server's reply (e.g. pipelining or a mocked stream returning null).

Common situations: Mock SMTP streams in unit tests whose ReadLine returns null at end-of-stream; custom transports that skip reading the final server response; wrappers that pass through a null response variable.

Related errors


AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15). Data as JSON: /api/errors/eced7692de910b81. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/MessageSentEventArgs.cs:59

		/// Initializes a new instance of the <see cref="MailKit.MessageSentEventArgs"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new <see cref="MessageSentEventArgs"/>.
		/// </remarks>
		/// <param name="message">The message that was just sent.</param>
		/// <param name="response">The response from the server.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <para><paramref name="message"/> is <see langword="null" />.</para>
		/// <para>-or-</para>
		/// <para><paramref name="response"/> is <see langword="null" />.</para>
		/// </exception>
		public MessageSentEventArgs (MimeMessage message, string response)
		{
			if (message == null)
				throw new ArgumentNullException (nameof (message));

			if (response == null)
				throw new ArgumentNullException (nameof (response));

			Message = message;
			Response = response;
		}

		/// <summary>
		/// Get the message that was just sent.
		/// </summary>
		/// <remarks>
		/// Gets the message that was just sent.
		/// </remarks>
		/// <value>The message.</value>
		public MimeMessage Message {
			get; private set;
		}

		/// <summary>
		/// Get the server's response.

View on GitHub (pinned to 9d3859a785)