jstedfast/MailKit · error · ArgumentException

Could not resolve a suitable IPv4 address for

Error message

Could not resolve a suitable IPv4 address for '{host}'.

What it means

SOCKS4 only supports IPv4. Socks4Client.Resolve performs DNS resolution of the proxy (or target) host and throws ArgumentException('Could not resolve a suitable IPv4 address for ...') if none of the resolved addresses has AddressFamily InterNetwork — e.g. the host only resolves to IPv6 (AAAA) addresses.

Solutions

  1. Use Socks5Client, which supports IPv6, instead of Socks4Client.
  2. Pass the proxy host as an IPv4 literal (e.g. 192.0.2.10) to bypass DNS.
  3. Fix DNS so an A record exists for the host, or use a different resolver.
  4. Check with `nslookup host` / `dig A host` that an IPv4 A record exists.
  5. Enable IPv4 (dual-stack) on the machine or network if it is disabled.

Example fix

// before
var proxy = new Socks4Client("proxy.internal", 1080); // only AAAA records -> throws
// after
var proxy = new Socks5Client("proxy.internal", 1080); // SOCKS5 handles IPv6
Defensive patterns

Strategy: validation

Validate before calling

var addrs = Dns.GetHostAddresses(host);
if (!addrs.Any(a => a.AddressFamily == AddressFamily.InterNetwork))
    throw new InvalidOperationException($"{host} has no IPv4 (A) record; SOCKS4 cannot be used");

Type guard

bool HasIPv4(string host) =>
    Dns.GetHostAddresses(host).Any(a => a.AddressFamily == AddressFamily.InterNetwork);

Try / catch

try {
    proxy.Connect(targetEp, cancellationToken);
} catch (ArgumentException ex) when (ex.Message.Contains("suitable IPv4 address")) {
    // switch to Socks5Client or an IPv4 literal
}

Prevention

When it happens

Trigger: Using Socks4Client with a host name that resolves exclusively to IPv6 addresses (dual-stack disabled for IPv4 or DNS returning only AAAA records).

Common situations: IPv6-only or heavily IPv6-preferring networks; internal DNS entries created as AAAA-only; 'localhost' resolving to ::1 first with no IPv4 mapping in some configurations; container/VM images without IPv4 DNS records.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at MailKit/Net/Proxy/Socks4Client.cs:140

		static string GetFailureReason (byte reply)
		{
			switch ((Socks4Reply) reply) {
			case Socks4Reply.RequestRejected:       return "Request rejected or failed.";
			case Socks4Reply.RequestFailedNoIdentd: return "Request failed; unable to contact client machine's identd service.";
			case Socks4Reply.RequestFailedWrongId:  return "Request failed; client ID does not match specified username.";
			default:                                return "Unknown error.";
			}
		}

		static IPAddress Resolve (string host, IPAddress[] ipAddresses)
		{
			for (int i = 0; i < ipAddresses.Length; i++) {
				if (ipAddresses[i].AddressFamily == AddressFamily.InterNetwork)
					return ipAddresses[i];
			}

			throw new ArgumentException ($"Could not resolve a suitable IPv4 address for '{host}'.", nameof (host));
		}

		static IPAddress Resolve (string host, CancellationToken cancellationToken)
		{
			cancellationToken.ThrowIfCancellationRequested ();

			var ipAddresses = Dns.GetHostAddresses (host);

			return Resolve (host, ipAddresses);
		}

		static async Task<IPAddress> ResolveAsync (string host, CancellationToken cancellationToken)
		{
			cancellationToken.ThrowIfCancellationRequested ();

#if NET6_0_OR_GREATER
			var ipAddresses = await Dns.GetHostAddressesAsync (host, cancellationToken).ConfigureAwait (false);
#else

View on GitHub (pinned to 9d3859a785)