jstedfast/MailKit · error · ArgumentException

The host name cannot be empty.

Error message

The host name cannot be empty.

What it means

CheckCanConnect validates arguments before Pop3Client.Connect proceeds: a null host throws ArgumentNullException and an empty (zero-length) host string throws ArgumentException("The host name cannot be empty."). MailKit cannot open a socket or form the SSL/TLS target name without a host, so an empty value is rejected up front.

Solutions

  1. Populate the host before calling Connect, e.g. host = config.Pop3Host; validate it is non-null and non-empty.
  2. If host comes from a URI, use uri.Host and verify it parsed correctly.
  3. Fail fast in app startup with a clear configuration error when the host setting is empty.
  4. Trim/normalize the value and reject empties at the settings-loading boundary.

Example fix

// before
var host = ConfigurationManager.AppSettings["Pop3Host"] ?? "";
client.Connect(host, 995, SecureSocketOptions.SslOnConnect, ct); // throws when empty

// after
var host = ConfigurationManager.AppSettings["Pop3Host"];
if (string.IsNullOrWhiteSpace(host))
    throw new InvalidOperationException("Pop3Host is not configured.");
client.Connect(host, 995, SecureSocketOptions.SslOnConnect, ct);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(host))
    throw new ArgumentException("POP3 host must be a non-empty string.", nameof(host));
if (host == null)
    throw new ArgumentNullException(nameof(host));

Type guard

bool IsValidHost(string? host) => !string.IsNullOrWhiteSpace(host);

Try / catch

try {
    client.Connect(host, port, SecureSocketOptions.SslOnConnect, ct);
} catch (ArgumentException ex) when (ex.Message.Contains("host name cannot be empty")) {
    logger.LogError("POP3 host setting is empty; check configuration.");
    throw;
}

Prevention

When it happens

Trigger: Calling Connect("", port, ...) or any Connect overload whose resolved host argument is the empty string — commonly because a config/settings object supplied an unset-but-not-null host value, or string manipulation (trim/split) produced "".

Common situations: Config files with Host=""; environment variables read as empty strings instead of null; UI forms submitted without a server field; defaults like string.Empty in options classes.

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/13506da4a03403f8. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/Net/Pop3/Pop3Client.cs:1093

				break;
			case SecureSocketOptions.SslOnConnect:
				uri = new Uri (string.Format (CultureInfo.InvariantCulture, "pops://{0}:{1}", host, port));
				starttls = false;
				break;
			default:
				uri = new Uri (string.Format (CultureInfo.InvariantCulture, "pop://{0}:{1}", host, port));
				starttls = false;
				break;
			}
		}

		void CheckCanConnect (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 Pop3Client is already connected.");
		}

		void SslHandshake (SslStream ssl, string host, CancellationToken cancellationToken)
		{
#if NET5_0_OR_GREATER
			ssl.AuthenticateAsClient (GetSslClientAuthenticationOptions (host, ValidateRemoteCertificate));
#else
			ssl.AuthenticateAsClient (host, ClientCertificates, SslProtocols, CheckCertificateRevocation);
#endif
		}

View on GitHub (pinned to 9d3859a785)