TnefPropertyReader Class

A forward-only reader for the MAPI properties contained within a TNEF attribute.

Definition

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

Remarks

A TnefPropertyReader provides forward-only access to the MAPI properties contained within the MapiProperties, Attachment and RecipientTable attributes. It is obtained by calling GetPropertyReader.

For a RecipientTable attribute, each row of properties is selected by calling ReadNextRow(CancellationToken) before reading the row's properties with ReadNextProperty(CancellationToken).

When positioned on a property, the reader is also positioned on the property's first value (if it has any). Additional values of a multi-valued property are selected using ReadNextValue(CancellationToken).

Fixed-width values (integers, floating point values, dates, booleans and GUIDs) are decoded when the reader is positioned on them and may be read any number of times. Variable-length values (strings, binary values and objects) may only be read once.

The property reader is only valid until the TnefReader is advanced to the next attribute. After that, any use of the property reader throws InvalidOperationException.

Like the TnefReader, the property reader never throws because of malformed data. Problems are reported to the reader's ComplianceLogger, and if the remainder of the attribute cannot be interpreted, the reader simply stops returning properties.

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 ());
    }
}
                  

Properties

IsEmbeddedMessage Get whether the current value is an embedded TNEF message.
IsMultiValued Get whether the current property is multi-valued.
IsValueConsumed Get whether the current variable-length value has already been read.
Name Get the name of the current property.
PropertyCount Get the number of properties.
PropertyType Get the type of the current property's values.
RowCount Get the number of table rows.
Tag Get the tag of the current property.
ValueCount Get the number of values of the current property.

Methods

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)
GetType Gets the Type of the current instance.
(Inherited from Object)
OpenEmbeddedMessage Open a reader for the embedded TNEF message contained within the current value.
OpenValueStream Open a stream for reading the current value.
ReadNextProperty Advance to the next property.
ReadNextPropertyAsync Asynchronously advance to the next property.
ReadNextRow Advance to the next row of a table.
ReadNextRowAsync Asynchronously advance to the next row of a table.
ReadNextValue Advance to the next value of the current property.
ReadNextValueAsync Asynchronously advance to the next value of the current property.
ReadProperty Read the current property.
ReadPropertyAsync Asynchronously read the current property.
ReadPropertySet Read the remaining properties.
ReadPropertySetAsync Asynchronously read the remaining properties.
ReadRowsAsPropertySets Read the remaining rows of a table.
ReadRowsAsPropertySetsAsync Asynchronously read the remaining rows of a table.
ReadValue Read the current value.
ReadValueAsBoolean Read the current value as a boolean.
ReadValueAsBytes Read the current value as a byte array.
ReadValueAsBytesAsync Asynchronously read the current value as a byte array.
ReadValueAsDateTime Read the current value as a date and time.
ReadValueAsDouble Read the current value as a double.
ReadValueAsFloat Read the current value as a float.
ReadValueAsGuid Read the current value as a GUID.
ReadValueAsInt16 Read the current value as a 16-bit integer.
ReadValueAsInt32 Read the current value as a 32-bit integer.
ReadValueAsInt64 Read the current value as a 64-bit integer.
ReadValueAsString Read the current value as a string.
ReadValueAsStringAsync Asynchronously read the current value as a string.
ReadValueAsync Asynchronously read the current value.
ToString Returns a string that represents the current object.
(Inherited from Object)

See Also