jstedfast/MailKit · error · ArgumentException

The host name cannot be empty.

Error message

The host name cannot be empty.

What it means

SmtpClient.Connect validates its arguments via ValidateArguments and throws ArgumentException when the host string is non-null but zero-length (""). A DNS/connect target cannot be empty, so this fails fast before any network activity.

Solutions

  1. Supply the actual SMTP hostname (e.g. "smtp.example.com") to Connect.
  2. Validate the configuration at startup and fail with a clear message when the host value is null/empty.
  3. Check environment-specific config files (appsettings.Production.json, env vars) for the missing host value.

Example fix

// before
var host = Configuration["Smtp:Host"] ?? "";
client.Connect(host, 587, SecureSocketOptions.StartTls); // ArgumentException

// after
var host = Configuration["Smtp:Host"];
if (string.IsNullOrWhiteSpace(host))
    throw new InvalidOperationException("Smtp:Host is not configured.");
client.Connect(host, 587, SecureSocketOptions.StartTls);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(host))
    throw new InvalidOperationException("SMTP host is not configured.");
if (port is < 0 or > 65535)
    throw new InvalidOperationException("SMTP port is out of range.");

Try / catch

try {
    client.Connect(host, port, options);
} catch (ArgumentException ex) when (ex.ParamName == "host") {
    // surface a configuration error naming the missing setting
    throw new MissingConfigException("Smtp host missing/empty", ex);
}

Prevention

When it happens

Trigger: Connect("", port, options) where the host came from an unset/empty configuration value, a missing appSetting/environment variable, or a subexpression that produced an empty string.

Common situations: Config keys like Smtp:Host missing so config["Smtp:Host"] returns null converted to ""; environment variables not set in the deployment environment but present locally; deserialized settings objects with empty defaults.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at MailKit/Net/Smtp/SmtpClient.cs:1409

				connected = true;
			} catch (Exception ex) {
				RecordClientDisconnected (ex);
				Stream.Dispose ();
				secure = false;
				Stream = null;
				throw;
			}

			OnConnected (host, port, options);
		}

		void ValidateArguments (string host, int port)
		{
			if (host == null)
				throw new ArgumentNullException (nameof (host));

			if (host.Length == 0)
				throw new ArgumentException ("The host name cannot be empty.", nameof (host));

			if (port < 0 || port > 65535)
				throw new ArgumentOutOfRangeException (nameof (port));

			CheckDisposed ();

			if (IsConnected)
				throw new InvalidOperationException ("The SmtpClient is already connected.");
		}

		/// <summary>
		/// Establish a connection to the specified SMTP or SMTP/S server.
		/// </summary>
		/// <remarks>
		/// <para>Establishes a connection to the specified SMTP or SMTP/S server.</para>
		/// <para>If the <paramref name="port"/> has a value of <c>0</c>, then the
		/// <paramref name="options"/> parameter is used to determine the default port to
		/// connect to. The default port used with <see cref="SecureSocketOptions.SslOnConnect"/>

View on GitHub (pinned to 9d3859a785)