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

emlContent string

The EML content string.

Returns

MailMessage

A new MailMessage populated from the EML content.

Exceptions

ArgumentNullException

emlContent is 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

emlContent string

The EML content string.

options EmlParserOptions

Parsing options. Set Strict to reject malformed input.

Returns

MailMessage

A new MailMessage populated from the EML content.

Exceptions

ArgumentNullException

emlContent or options is 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

filePath string

The path to the EML file.

Returns

MailMessage

A new MailMessage populated from the file.

Exceptions

ArgumentNullException

filePath is null.

ParseFile(string, EmlParserOptions)

Parses an EML file into a MailMessage using the supplied options.

public static MailMessage ParseFile(string filePath, EmlParserOptions options)

Parameters

filePath string

The path to the EML file.

options EmlParserOptions

Parsing options. Set Strict to reject malformed input.

Returns

MailMessage

A new MailMessage populated from the file.

Exceptions

ArgumentNullException

filePath or options is 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

filePath string

The path to the EML file.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

Task<MailMessage>

A task containing the parsed MailMessage.

Exceptions

ArgumentNullException

filePath is 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

filePath string

The path to the EML file.

options EmlParserOptions

Parsing options. Set Strict to reject malformed input.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

Task<MailMessage>

A task containing the parsed MailMessage.

Exceptions

ArgumentNullException

filePath or options is 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

emlContent string

The EML content string.

options EmlParserOptions

Optional 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

emlContent is null.

EmlParseException

A malformed construct was encountered and Strict is set.