TnefMessageLoad(TnefReader, CancellationToken) Method

Load a TNEF message using the specified reader.

Definition

Namespace: MimeKit.Tnef
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
C#
public static TnefMessage Load(
	TnefReader reader,
	CancellationToken cancellationToken = default
)

Parameters

reader  TnefReader
The TNEF reader.
cancellationToken  CancellationToken  (Optional)
The cancellation token.

Return Value

TnefMessage
The TNEF message.

Remarks

Loads a TNEF message using the specified reader, which allows the caller to configure the reader's ComplianceLogger.

The reader must not have been advanced. It is not disposed.

Example

C#
                    class TnefComplianceCollector : ITnefComplianceLogger
{
    public List<TnefComplianceIssue> Issues { get; } = new List<TnefComplianceIssue> ();

    public void Log (in TnefComplianceIssue issue)
    {
        Issues.Add (issue);
    }
}

class StrictTnefComplianceLogger : ITnefComplianceLogger
{
    public void Log (in TnefComplianceIssue issue)
    {
        // Reject anything that is likely to cause real problems; tolerate the rest.
        if (issue.Severity >= MimeComplianceSeverity.Major)
            throw new TnefException (issue.Violation, issue.Description);
    }
}

public static TnefMessage LoadAndReportIssues (Stream stream)
{
    var collector = new TnefComplianceCollector ();
    TnefMessage tnef;

    // The reader never throws for malformed TNEF; it recovers as best it can and reports each problem
    // to the ComplianceLogger. MaxComplianceIssuesPerViolation limits how many times each kind of
    // violation is reported.
    using (var reader = new TnefReader (stream, null, leaveOpen: true) { ComplianceLogger = collector, MaxComplianceIssuesPerViolation = 16 })
        tnef = TnefMessage.Load (reader);

    foreach (var issue in collector.Issues)
        Console.WriteLine ("{0} ({1}) @ offset {2}: {3}", issue.Violation, issue.Severity, issue.StreamOffset, issue.Description);

    return tnef;
}

public static TnefMessage LoadStrict (Stream stream)
{
    using (var reader = new TnefReader (stream, null, leaveOpen: true) { ComplianceLogger = new StrictTnefComplianceLogger () })
        return TnefMessage.Load (reader);
}
                  

Exceptions

ArgumentNullException reader is .
ObjectDisposedException reader has been disposed.
InvalidOperationException reader has already been advanced.
TnefException The stream does not begin with the TNEF signature.
OperationCanceledException The operation was canceled via the cancellation token.
IOException An I/O error occurred.

See Also