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
- Use Socks5Client, which supports IPv6, instead of Socks4Client.
- Pass the proxy host as an IPv4 literal (e.g. 192.0.2.10) to bypass DNS.
- Fix DNS so an A record exists for the host, or use a different resolver.
- Check with `nslookup host` / `dig A host` that an IPv4 A record exists.
- 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
- Prefer Socks5Client unless SOCKS4 is explicitly required
- Verify the proxy host has a DNS A record (dig/nslookup)
- Pass IPv4 literals in IPv6-only environments
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
- The specified host address must be IPv4.
- Failed to connect to
- Failed to connect to
- The length of the host name must be between 0 and 256…
- Value cannot be null. (Parameter 'name')
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);
#elseView on GitHub (pinned to 9d3859a785)