TnefReaderReadAsync Method

Asynchronously advance to the next attribute.

Definition

Namespace: MimeKit.Tnef
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
C#
public Task<bool> ReadAsync(
	CancellationToken cancellationToken = default
)

Parameters

cancellationToken  CancellationToken  (Optional)
The cancellation token.

Return Value

TaskBoolean
if the reader was advanced to the next attribute; otherwise, if there are no more attributes.

Remarks

Asynchronously advances the reader to the next attribute in the TNEF stream.

The first call reads the TNEF stream header. If the stream does not begin with the TNEF signature, a InvalidSignature issue is reported and the method returns .

Any part of the current attribute's value that has not been consumed is skipped, and the attribute's checksum is verified.

Example

C#
                    public static async Task<string> GetSubjectAsync (Stream stream, CancellationToken cancellationToken = default)
{
    string subject = null;

    using (var reader = new TnefReader (stream, null, leaveOpen: true)) {
        while (await reader.ReadAsync (cancellationToken)) {
            if (reader.Level != TnefAttributeLevel.Message)
                break;

            if (reader.Tag == TnefAttributeTag.Subject) {
                subject ??= await reader.ReadValueAsStringAsync (cancellationToken);
            } else if (reader.Tag == TnefAttributeTag.MapiProperties) {
                var prop = reader.GetPropertyReader ();

                while (await prop.ReadNextPropertyAsync (cancellationToken)) {
                    // Note: PidTagSubject, if present, takes precedence over the legacy attSubject attribute.
                    if (prop.Tag.Id == TnefPropertyId.Subject && prop.ValueCount > 0)
                        subject = await prop.ReadValueAsStringAsync (cancellationToken);
                }
            }
        }
    }

    return subject;
}
                  

Exceptions

ObjectDisposedException The reader has been disposed.
OperationCanceledException The operation was canceled via the cancellation token.
IOException An I/O error occurred.

See Also