TnefReader Class

A forward-only reader for TNEF streams.

Definition

Namespace: MimeKit.Tnef
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
C#
public sealed class TnefReader : IDisposable
Inheritance
Object    TnefReader
Implements
IDisposable

Remarks

A TnefReader provides forward-only, pull-style access to the attributes of a TNEF (Transport Neutral Encapsulation Format) stream as defined by [MS-OXTNEF].

Each call to Read(CancellationToken) advances the reader to the next attribute. The value of the current attribute may be consumed using one of the ReadValueAs* methods, OpenValueStream, or, for attributes that contain MAPI properties, GetPropertyReader. Any part of the value that is not consumed is skipped automatically by the next call to Read(CancellationToken).

The reader never throws because of malformed data. Instead, each problem that is detected is reported to the ComplianceLogger (if any) and the reader recovers as best it can. To stop at the first problem, use a logger that throws.

Example

C#
                    public static void DumpTnef (Stream stream)
{
    using (var reader = new TnefReader (stream, null, leaveOpen: true))
        DumpAttributes (reader);
}

static void DumpAttributes (TnefReader reader)
{
    var indent = new string (' ', reader.Depth * 4);

    while (reader.Read ()) {
        Console.WriteLine ("{0}{1} {2} ({3} bytes) @ offset {4}", indent, reader.Level, reader.Tag, reader.Length, reader.StreamOffset);

        switch (reader.Tag) {
        case TnefAttributeTag.MessageClass:
        case TnefAttributeTag.Subject:
            Console.WriteLine ("{0}    {1}", indent, reader.ReadValueAsString ());
            break;
        case TnefAttributeTag.DateSent:
        case TnefAttributeTag.DateReceived:
            Console.WriteLine ("{0}    {1}", indent, reader.ReadValueAsDateTime ());
            break;
        case TnefAttributeTag.RecipientTable:
            var recipients = reader.GetPropertyReader ();

            // The recipient table is a list of rows, each of which has its own list of properties.
            while (recipients.ReadNextRow ()) {
                Console.WriteLine ("{0}    Recipient:", indent);
                DumpProperties (recipients, indent + "        ");
            }
            break;
        case TnefAttributeTag.MapiProperties:
        case TnefAttributeTag.Attachment:
            DumpProperties (reader.GetPropertyReader (), indent + "    ");
            break;
        }

        // Note: There is no need to read the value of every attribute; any value that is
        // not read is skipped (and its checksum is still verified) by the next Read ().
    }
}

static void DumpProperties (TnefPropertyReader prop, string indent)
{
    while (prop.ReadNextProperty ()) {
        var name = prop.Name.HasValue ? prop.Name.Value.ToString () : prop.Tag.Id.ToString ();

        if (prop.IsEmbeddedMessage) {
            Console.WriteLine ("{0}{1} = <embedded message>", indent, name);

            using (var embedded = prop.OpenEmbeddedMessage ())
                DumpAttributes (embedded);
            continue;
        }

        if (prop.ValueCount == 0) {
            Console.WriteLine ("{0}{1} = <no values>", indent, name);
            continue;
        }

        // Note: The reader is already positioned on the first value of the property.
        do {
            var value = prop.ReadValue ();

            if (value is byte[] bytes)
                Console.WriteLine ("{0}{1} = <{2} bytes>", indent, name, bytes.Length);
            else
                Console.WriteLine ("{0}{1} = {2}", indent, name, value);
        } while (prop.ReadNextValue ());
    }
}
                  

Constructors

TnefReader Initialize a new instance of the TnefReader class.

Properties

AttributeType Get the type of the current attribute's value.
Codepage Get the codepage used to decode 8-bit strings.
ComplianceLogger Get or set the logger to use for reporting TNEF compliance violations.
ComplianceOptions Get or set the options that control how TNEF compliance violations are reported.
Depth Get the nesting depth of the reader.
LegacyKey Get the legacy key from the TNEF stream header.
Length Get the length of the current attribute's value.
Level Get the level of the current attribute.
Options Get the options used by the reader.
StreamOffset Get the stream offset of the current attribute.
Tag Get the tag of the current attribute.

Methods

Dispose Release all resources used by the TnefReader object.
Equals Determines whether the specified object is equal to the current object.
(Inherited from Object)
GetHashCode Serves as the default hash function.
(Inherited from Object)
GetPropertyReader Get a reader for the MAPI properties contained within the current attribute.
GetType Gets the Type of the current instance.
(Inherited from Object)
OpenValueStream Open a stream for reading the current attribute's raw value.
Read Advance to the next attribute.
ReadAsync Asynchronously advance to the next attribute.
ReadValueAsBytes Read the current attribute's raw value.
ReadValueAsBytesAsync Asynchronously read the current attribute's raw value.
ReadValueAsDateTime Read the current attribute's value as a date and time.
ReadValueAsDateTimeAsync Asynchronously read the current attribute's value as a date and time.
ReadValueAsInt16 Read the current attribute's value as a 16-bit integer.
ReadValueAsInt16Async Asynchronously read the current attribute's value as a 16-bit integer.
ReadValueAsInt32 Read the current attribute's value as a 32-bit integer.
ReadValueAsInt32Async Asynchronously read the current attribute's value as a 32-bit integer.
ReadValueAsString Read the current attribute's value as a string.
ReadValueAsStringAsync Asynchronously read the current attribute's value as a string.
ToString Returns a string that represents the current object.
(Inherited from Object)

See Also