TnefMessageLoad(Stream, TnefOptions, CancellationToken) Method

Load a TNEF message from the specified stream.

Definition

Namespace: MimeKit.Tnef
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
C#
public static TnefMessage Load(
	Stream stream,
	TnefOptions? options = null,
	CancellationToken cancellationToken = default
)

Parameters

stream  Stream
The TNEF stream.
options  TnefOptions  (Optional)
The options to use, or to use Default.
cancellationToken  CancellationToken  (Optional)
The cancellation token.

Return Value

TnefMessage
The TNEF message.

Remarks

Loads a TNEF message from the specified stream.

The stream is left open.

Example

C#
                    public static void PrintMessageProperties (string fileName)
{
    using (var stream = File.OpenRead (fileName))
    using (var tnef = TnefMessage.Load (stream)) {
        // The most commonly used properties have convenience accessors.
        Console.WriteLine ("Message-Class: {0}", tnef.MessageClass);
        Console.WriteLine ("Subject: {0}", tnef.Subject);
        Console.WriteLine ("Message-Id: {0}", tnef.InternetMessageId);

        // Any other property may be looked up by its tag. Note that a string lookup matches both the
        // String8 and the Unicode variant of the property tag.
        var sender = tnef.Properties.GetString (TnefPropertyTag.SenderEmailAddressW);
        var submitted = tnef.Properties.GetDateTime (TnefPropertyTag.ClientSubmitTime);
        var importance = tnef.Properties.GetInt32 (TnefPropertyTag.Importance);

        Console.WriteLine ("Sender: {0}", sender);
        Console.WriteLine ("Submitted: {0} (UTC)", submitted);
        Console.WriteLine ("Importance: {0}", importance);

        // Named properties are looked up by their TnefNameId.
        if (tnef.Properties.TryGetValue (TnefNameId.Location, out var location) && location.TryGetString (out var where))
            Console.WriteLine ("Location: {0}", where);

        // Multi-valued properties have more than one value.
        if (tnef.Properties.TryGetValue (TnefNameId.Keywords, out var keywords) && keywords.TryGetValues<string> (out var categories))
            Console.WriteLine ("Categories: {0}", string.Join (", ", categories));

        // Or simply enumerate every property.
        foreach (var property in tnef.Properties) {
            var name = property.Name.HasValue ? property.Name.Value.ToString () : property.Tag.Id.ToString ();
            var value = property.Value;

            if (value is byte[] bytes)
                value = string.Format ("<{0} bytes>", bytes.Length);
            else if (value is Array array)
                value = string.Join (", ", array.Cast<object> ());

            Console.WriteLine ("{0} ({1}) = {2}", name, property.PropertyType, value);
        }
    }
}
                  

Exceptions

ArgumentNullException stream is .
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