FormatException: System.FormatException: The header name has an unexpected characters, on "MailMessag.Headers" function Version of Aspose.Email(3.3)

Is there a way to diagnose which Header Name triggers it? How can I tell which one is it?

The list of Headers:
Received:
DKIM-Signature:
X-CSE-ConnectionGUID:
X-CSE-MsgGUID:
Message-Id:
X-Amp-Result:
Received:
From:
To:
Subject:
Date:
Content-Type:
Return-Path:
X-MS-Exchange-Organization-Network-Message-Id:
X-MS-Exchange-Organization-AuthSource:
X-MS-Exchange-Organization-AuthAs:
X-MS-Exchange-Organization-AuthMechanism:
X-MS-Exchange-Organization-AVStamp-Enterprise:
X-MS-Exchange-Transport-EndToEndLatency:
X-MS-Exchange-Processed-By-BccFoldering:
MIME-Version:
Content-Disposition:
Content-Transfer-Encoding:
Content-ID:
Content-Type:

Uid:
Date: (invalid)
DateCreated: 11/27/2025 15:31:13
DateRecieved: 11/27/2025 15:31:14
HtmlBody:
PlainText:
Size:
To:
Subject:
Content-Type:
MIME-Version: 1.0

Doesn’t look like there should be a problem here.

@JoaquimRaurell

Summary
MailMessage.Headers throws a System.FormatException when it encounters a header name that does not meet the RFC‑822/5322 token rules. The exception does not include the name of the offending header, so you have to locate it yourself.

Why it happens
In Aspose.Email 3.3 the header collection is validated strictly. A header name that contains illegal characters (e.g., control characters, spaces, or other non‑ASCII symbols) or is otherwise malformed will cause the parser to raise the exception and stop processing.

What to do next

  1. Parse the raw header block yourself and add each header individually, catching the exception for each line. This will reveal the exact line that triggers the error.
// rawHeaders – the full header text as it appears in the email (including line breaks)
var lines = rawHeaders.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
var msg = new MailMessage();

foreach (var line in lines)
{
 if (string.IsNullOrWhiteSpace(line)) continue;

 int idx = line.IndexOf(':');
 if (idx <= 0) continue; // malformed line – optionally log it

 string name = line.Substring(0, idx).Trim();
 string value = line.Substring(idx + 1).Trim();

 try
 {
 msg.Headers.Add(name, value); // throws if name is illegal
 }
 catch (FormatException)
 {
 Console.WriteLine($"Problematic header: \"{line}\"");
 // optionally break after the first offending header
 }
}

The console output will show the exact header line that caused the exception.

  1. Inspect the reported line for hidden or non‑printable characters, unexpected spaces, or duplicate header names. Removing or correcting the problematic characters should allow the message to be loaded.

  2. Validate header names before adding them (e.g., ensure they consist only of printable ASCII characters) if you need to pre‑filter the data.

  3. Consider upgrading to a newer Aspose.Email version if possible; later releases may have improved validation or more informative error messages.

By manually iterating over the headers and catching the exception, you can pinpoint the offending header and correct it.

:warning: The above steps are based on typical usage of Aspose.Email and the behavior described in the forum question. Please test in your environment to confirm the exact cause.

:warning: I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.

Hi @JoaquimRaurell

We checked the headers you provided with code above, but were unable to reproduce the issue you described. Please check you scenario with the latest version of Aspose.Email. If the issue persists, please provide the source file and the code you used for more deep investigation.