Class EmlParser
- Namespace
- Wolfgang.Extensions.Mail
- Assembly
- Wolfgang.Extensions.Mail.dll
Provides methods to parse EML (RFC 2822 MIME) content into MailMessage objects.
public static class EmlParser
- Inheritance
-
EmlParser
- Inherited Members
Examples
using var message = await EmlParser.ParseFileAsync("message.eml");
Console.WriteLine(message.Subject);
Remarks
Parsing is deliberately lenient, because real-world EML files routinely
contain malformed headers: an address that cannot be parsed is skipped
rather than throwing, and a malformed From header leaves
From null. To detect what a lenient
parse dropped, pair with
Validate(MailMessage), which reports
a missing sender or recipients as structured issues.
Methods
Parse(string)
Parses a raw EML/MIME string into a MailMessage using the default lenient behavior (malformed constructs are skipped).
public static MailMessage Parse(string emlContent)
Parameters
emlContentstringThe EML content string.
Returns
- MailMessage
A new MailMessage populated from the EML content.
Exceptions
- ArgumentNullException
emlContentis null.
Parse(string, EmlParserOptions)
Parses a raw EML/MIME string into a MailMessage using the
supplied options.
public static MailMessage Parse(string emlContent, EmlParserOptions options)
Parameters
emlContentstringThe EML content string.
optionsEmlParserOptionsParsing options. Set Strict to reject malformed input.
Returns
- MailMessage
A new MailMessage populated from the EML content.
Exceptions
- ArgumentNullException
emlContentoroptionsis null.- EmlParseException
A malformed construct was encountered and Strict is set.
ParseFile(string)
Parses an EML file into a MailMessage.
public static MailMessage ParseFile(string filePath)
Parameters
filePathstringThe path to the EML file.
Returns
- MailMessage
A new MailMessage populated from the file.
Exceptions
- ArgumentNullException
filePathis null.
ParseFile(string, EmlParserOptions)
Parses an EML file into a MailMessage using the supplied
options.
public static MailMessage ParseFile(string filePath, EmlParserOptions options)
Parameters
filePathstringThe path to the EML file.
optionsEmlParserOptionsParsing options. Set Strict to reject malformed input.
Returns
- MailMessage
A new MailMessage populated from the file.
Exceptions
- ArgumentNullException
filePathoroptionsis null.- EmlParseException
A malformed construct was encountered and Strict is set.
ParseFileAsync(string, CancellationToken)
Asynchronously parses an EML file into a MailMessage.
public static Task<MailMessage> ParseFileAsync(string filePath, CancellationToken cancellationToken = default)
Parameters
filePathstringThe path to the EML file.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- Task<MailMessage>
A task containing the parsed MailMessage.
Exceptions
- ArgumentNullException
filePathis null.
ParseFileAsync(string, EmlParserOptions, CancellationToken)
Asynchronously parses an EML file into a MailMessage using
the supplied options.
public static Task<MailMessage> ParseFileAsync(string filePath, EmlParserOptions options, CancellationToken cancellationToken = default)
Parameters
filePathstringThe path to the EML file.
optionsEmlParserOptionsParsing options. Set Strict to reject malformed input.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- Task<MailMessage>
A task containing the parsed MailMessage.
Exceptions
- ArgumentNullException
filePathoroptionsis null.- EmlParseException
A malformed construct was encountered and Strict is set.
ParseWithDiagnostics(string, EmlParserOptions?)
Parses a raw EML/MIME string and returns both the message and the list of malformed constructs the lenient parser skipped.
public static ParseResult ParseWithDiagnostics(string emlContent, EmlParserOptions? options = null)
Parameters
emlContentstringThe EML content string.
optionsEmlParserOptionsOptional parsing options. When Strict is set, a malformed construct throws instead of being reported.
Returns
- ParseResult
A ParseResult containing the message and any diagnostics.
Exceptions
- ArgumentNullException
emlContentis null.- EmlParseException
A malformed construct was encountered and Strict is set.