jstedfast/MailKit · error · ArgumentException

The host name cannot be empty.

Error message

The host name cannot be empty.

What it means

Connect(string host, int port, bool useSsl) requires a non-empty host string to resolve via DNS. An empty string cannot be a host name, so MailKit throws ArgumentException rather than attempting a DNS lookup that would inevitably fail.

Solutions

  1. Provide the actual SMTP/IMAP host, e.g. Connect("smtp.example.com", 587, true)
  2. Fix the config source so the host key is populated (appsettings.json, env var)
  3. Guard in code: if (string.IsNullOrEmpty(host)) fail with a clear config error before calling Connect

Example fix

// before
string host = config["SMTP_HOST"] ?? "";
client.Connect(host, 587, true);
// after
string host = config["SMTP_HOST"] ?? "smtp.example.com";
client.Connect(host, 587, true);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(host))
    throw new InvalidOperationException("SMTP host is not configured");
client.Connect(host, port, useSsl);

Type guard

static bool HasHost(string host) => !string.IsNullOrWhiteSpace(host);

Try / catch

try { client.Connect(host, port, useSsl); }
catch (ArgumentException ex) when (ex.Message.Contains("host name cannot be empty")) { throw new ConfigurationException("SMTP host is missing — check SMTP_HOST/appsettings", ex); }

Prevention

When it happens

Trigger: Calling Connect with host == string.Empty, e.g. Connect("", 587, true).

Common situations: Missing or misnamed key in configuration producing an empty default (host = config["SmtpHost"] ?? ""), environment variable not set, or appsettings/env mismatch after deployment.

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/0d71311bf149e2e4. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/MailService.cs:955

		/// <exception cref="System.OperationCanceledException">
		/// The operation was canceled via the cancellation token.
		/// </exception>
		/// <exception cref="System.Net.Sockets.SocketException">
		/// A socket error occurred trying to connect to the remote host.
		/// </exception>
		/// <exception cref="System.IO.IOException">
		/// An I/O error occurred.
		/// </exception>
		/// <exception cref="ProtocolException">
		/// A protocol error occurred.
		/// </exception>
		public void Connect (string host, int port, bool useSsl, CancellationToken cancellationToken = default)
		{
			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));

			Connect (host, port, useSsl ? SecureSocketOptions.SslOnConnect : SecureSocketOptions.StartTlsWhenAvailable, cancellationToken);
		}

		/// <summary>
		/// Asynchronously establish a connection to the specified mail server.
		/// </summary>
		/// <remarks>
		/// <para>Asynchronously establishes a connection to the specified mail server.</para>
		/// <note type="note">
		/// <para>The <paramref name="useSsl"/> argument only controls whether or
		/// not the client makes an SSL-wrapped connection. In other words, even if the
		/// <paramref name="useSsl"/> parameter is <see langword="false" />, SSL/TLS may still be used if
		/// the mail server supports the STARTTLS extension.</para>
		/// <para>To disable all use of SSL/TLS, use the

View on GitHub (pinned to 9d3859a785)