Recursive AI

Parse an Authentication-Results header in C#

OneMail is a free, open-source email library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Authentication-Results records a mail server’s reported checks. The zip’s message.eml has example SPF, DKIM and DMARC results.

Terminal
dotnet add package RecursiveAI.OneMail

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneMail.Mime;
 
var message = MimeEntity.Parse(
    File.ReadAllBytes("message.eml"));
var headers =
    message.GetAuthenticationResults();
foreach (var header in headers)
    foreach (var result in header.Results)
        Console.WriteLine(
            $"{result.Method}={result.Result}");

Have only the header value? Use AuthenticationResults.Parse in the variation.

Text
spf=pass
dkim=pass
dmarc=pass

Unzip the example, then dotnet run.

The full program

The sample message has a plain-text body and this folded header:

Text
Authentication-Results: mx.example.net;
 spf=pass smtp.mailfrom=sender@example.com;
 dkim=pass (good signature) header.d=example.com;
 dmarc=pass header.from=example.com

The full program also prints the reporting service, the method properties and the number of header parse defects. This is parsing only: nothing is evaluated or trusted; check the authserv-id (the name of the service reporting the checks) against your own receiving boundary before relying on the reported results.

C#
using RecursiveAI.OneMail.Mime;
 
var message = MimeEntity.Parse(File.ReadAllBytes("message.eml"));
foreach (var header in message.GetAuthenticationResults())
{
    Console.WriteLine($"Reported by: {header.AuthServId}");
    foreach (var result in header.Results)
    {
        Console.WriteLine($"{result.Method}={result.Result}");
        foreach (var property in result.Properties)
            Console.WriteLine($"  {property}");
    }
    Console.WriteLine($"Parse defects: {header.Defects.Count}");
}

dotnet run prints:

Text
Reported by: mx.example.net
spf=pass
  smtp.mailfrom=sender@example.com
dkim=pass
  header.d=example.com
dmarc=pass
  header.from=example.com
Parse defects: 0

Malformed header content is returned as far as it can be parsed, with defects describing what was skipped or guessed.

Variations

Parse a field value directly. Pass only the text after Authentication-Results:, not the field name.

C#
using RecursiveAI.OneMail.Mime.Authentication;
 
var header = AuthenticationResults.Parse(
    "mx.example.net; dkim=pass header.d=example.com");
Console.WriteLine(header.AuthServId);
Console.WriteLine(header.Results[0].GetProperty("header", "d"));
Text
mx.example.net
example.com

The short answer

Parse the message as a MIME tree and call GetAuthenticationResults() to obtain every Authentication-Results field. Each field exposes the reporting service as AuthServId and a list of method results. Read Method, Result and Properties rather than splitting the header on spaces or semicolons.

This is a parser for recorded claims, not an SPF evaluator, DKIM verifier or DMARC policy engine. The OneMail documentation explicitly says that nothing is evaluated or trusted. It needs no cryptography package or DNS request. The sample is a local message whose pass results were written as text, not obtained from a live receiving server.

What is SPF and DKIM authentication?

SPF and DKIM are distinct email authentication mechanisms. At a high level, SPF checks whether a sending host is authorized for a domain, while DKIM verifies a signature associated with a signing domain. Their recorded results can appear together in Authentication-Results; the field itself is a report of checks, not the check operation. RFC 8601, section 2.7 defines the registered authentication method result forms.

In the sample, spf=pass carries smtp.mailfrom=sender@example.com. The DKIM result carries header.d=example.com, and the DMARC result carries header.from=example.com. These are properties of different methods. Keep the type and property name together when presenting them; a bare domain string loses the information about which identity was checked.

The full program prints that structure without trying to infer more than the header says. For a structured export, keep the authserv-id, method, result, reason, comments and properties separately. The typed view also exposes method versions and a result-kind enum. Preserve the original result text when you encounter an unfamiliar keyword instead of treating every unknown value as a failure.

What does DKIM authenticated mean?

A dkim=pass result says that the service named in that Authentication-Results field reports a passing DKIM check. It does not mean that calling this parser verified the signature. Nor does a syntactically valid field establish that its author is your trusted mail receiver.

RFC 8601, section 7 discusses the trust boundary and forged result fields. An application needs a receiving pipeline and policy that identify which fields came from its trusted infrastructure. Checking that an arbitrary uploaded message contains your receiver’s name is not, by itself, evidence that your receiver wrote the field.

Multiple fields may describe different handling stages. The API returns every such field; do not collapse them into one global pass flag or take the first passing DKIM clause regardless of source. The none form means the named service made no checks, not that a signature passed or failed.

The parser tolerates malformed content and reports defects with offsets for what it skipped or guessed. That permits inspection of imperfect mail, but it is another reason to keep parsing separate from an authorization decision. Treat reported results and parse defects as separate data. If a message contains no Authentication-Results field, the loop prints nothing; absence is not a failed cryptographic verification. For one standalone header value, use AuthenticationResults.Parse as in the variation rather than constructing a synthetic whole message.

All onemail recipes