jstedfast/MailKit · error · ArgumentNullException

Value cannot be null. (Parameter 'message')

Error message

Value cannot be null. (Parameter 'message')

What it means

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.

Solutions

  1. Ensure the MimeMessage is fully constructed before sending / raising the event; never call Send(null, ...).
  2. Guard before constructing: `if (message == null) return;` or throw your own descriptive error.
  3. If the message is null because delivery failed, handle the send failure path instead of emitting a MessageSent event.

Example fix

// before
smtp.MessageSent += (s, e) => { }; // event raised with null message internally
smtp.Send(null, "250 OK");
// after
var message = MimeMessage.CreateFromMailMessage(mailMessage);
if (message != null)
    smtp.Send(message, "250 OK");
Defensive patterns

Strategy: validation

Validate before calling

if (message == null)
    throw new InvalidOperationException("Cannot send: MimeMessage is null.");
var args = new MessageSentEventArgs(message, response);

Type guard

bool IsValidMessage(MimeMessage message) => message != null;

Try / catch

try {
    var args = new MessageSentEventArgs(message, response);
} catch (ArgumentNullException ex) when (ex.ParamName == "message") {
    // log and skip: nothing was actually sent
    logger.LogWarning("MessageSent event skipped: message was null");
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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

Appendix: source

Thrown at MailKit/MessageSentEventArgs.cs:56

	public class MessageSentEventArgs : EventArgs
	{
		/// <summary>
		/// 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;
		}

View on GitHub (pinned to 9d3859a785)