Tnef Writer Class
Definition
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
public sealed class TnefWriter : IDisposable- Inheritance
- Object TnefWriter
- Implements
- IDisposable
Remarks
A TnefWriter produces a TNEF (Transport Neutral Encapsulation Format) stream as defined by [MS-OXTNEF].
The stream header, the TnefVersion attribute and the OemCodepage attribute are written automatically. The level of each attribute is determined by its tag, and the length and checksum of each attribute are calculated automatically.
The writer is strict: it throws rather than produce a stream that does not conform to the grammar defined by [MS-OXTNEF] section 2.1.3.1. All message-level attributes must be written before any attachments and MapiProperties, if written, must be the last message-level attribute. Each attachment must begin with a AttachRenderData attribute and Attachment, if written, must be the last attribute of the attachment.
Attributes that are written using OpenAttributeStream(TnefAttributeTag) or OpenPropertyWriter(TnefAttributeTag) are buffered using TemporaryStreamFactory until the stream or property writer is disposed and are then written to the output stream by the next call to one of the WriteAttribute methods, Flush, or Dispose.
Example
public static void WriteTnef (Stream output, byte[] rtf, string attachmentFileName, Stream attachmentContent)
{
// The TnefWriter writes the TNEF signature, attTnefVersion and attOemCodepage itself, computes
// every length, count, checksum and padding, and throws InvalidOperationException if attributes
// are written in an order that [MS-OXTNEF] does not allow.
using (var writer = new TnefWriter (output, 1252, 0, leaveOpen: true)) {
// Message-level attributes come first...
writer.WriteAttribute (TnefAttributeTag.MessageClass, "IPM.Note");
// ...followed by the recipient table, which has a row of properties for each recipient...
using (var recipients = writer.OpenPropertyWriter (TnefAttributeTag.RecipientTable)) {
recipients.BeginRow ();
recipients.WritePropertyTag (TnefPropertyTag.RecipientType);
recipients.WriteValue ((int) TnefRecipientType.To);
recipients.WritePropertyTag (TnefPropertyTag.DisplayNameW);
recipients.WriteValue ("Alice");
recipients.WritePropertyTag (TnefPropertyTag.AddrtypeW);
recipients.WriteValue ("SMTP");
recipients.WritePropertyTag (TnefPropertyTag.EmailAddressW);
recipients.WriteValue ("alice@example.com");
}
// ...and the message's MAPI properties, which must be the last message-level attribute.
using (var properties = writer.OpenPropertyWriter (TnefAttributeTag.MapiProperties)) {
properties.WritePropertyTag (TnefPropertyTag.SubjectW);
properties.WriteValue ("Quarterly report");
// Note: PT_SYSTIME property values are always in UTC.
properties.WritePropertyTag (TnefPropertyTag.ClientSubmitTime);
properties.WriteValue (DateTime.UtcNow);
// The RTF body is compressed as it is written.
properties.WritePropertyTag (TnefPropertyTag.RtfCompressed);
using (var body = properties.OpenRtfCompressedStream ())
body.Write (rtf, 0, rtf.Length);
// Named properties are assigned property ids (0x8000 and up) by the writer.
properties.WritePropertyTag (TnefNameId.Keywords, TnefPropertyType.Unicode | TnefPropertyType.MultiValued);
properties.WriteValue ("finance");
properties.WriteValue ("q3");
}
// Each attachment begins with an attAttachRendData attribute: the attachment type (1 = file),
// its position in the body (-1 = not rendered), its width and height, and its flags.
var renderData = new byte[] { 0x01, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
writer.WriteAttribute (TnefAttributeTag.AttachRenderData, renderData);
// ...and ends with its MAPI properties.
using (var properties = writer.OpenPropertyWriter (TnefAttributeTag.Attachment)) {
properties.WritePropertyTag (TnefPropertyTag.AttachMethod);
properties.WriteValue ((int) TnefAttachMethod.ByValue);
properties.WritePropertyTag (TnefPropertyTag.AttachLongFilenameW);
properties.WriteValue (attachmentFileName);
properties.WritePropertyTag (TnefPropertyTag.AttachDataBin);
using (var content = properties.OpenValueStream ())
attachmentContent.CopyTo (content);
}
writer.Flush ();
}
}
Constructors
| TnefWriter | Initialize a new instance of the TnefWriter class. |
Properties
| Codepage | Get the codepage used to encode 8-bit strings. |
| LegacyKey | Get the legacy key. |
| TemporaryStreamFactory | Get or set the factory used to create temporary streams for buffering attributes. |
Methods
| Dispose | Release all resources used by the TnefWriter object. |
| Equals | Determines whether the specified object is equal to the current object. (Inherited from Object) |
| Flush | Flush the writer. |
| FlushAsync | Asynchronously flush the writer. |
| GetHashCode | Serves as the default hash function. (Inherited from Object) |
| GetType | Gets the Type of the current instance. (Inherited from Object) |
| OpenAttributeStream | Open a stream for writing the raw value of an attribute. |
| OpenPropertyWriter | Open a property writer for an attribute that contains MAPI properties. |
| ToString | Returns a string that represents the current object. (Inherited from Object) |
| WriteAttribute(TnefAttributeTag, Byte) | Write an attribute with a raw value. |
| WriteAttribute(TnefAttributeTag, DateTime) | Write an attribute with a date value. |
| WriteAttribute(TnefAttributeTag, Int32) | Write an attribute with an integer value. |
| WriteAttribute(TnefAttributeTag, String) | Write an attribute with a string value. |
| WriteAttribute(TnefAttributeTag, Byte, Int32, Int32) | Write an attribute with a raw value. |
| WriteAttributeAsync(TnefAttributeTag, Byte, CancellationToken) | Asynchronously write an attribute with a raw value. |
| WriteAttributeAsync(TnefAttributeTag, DateTime, CancellationToken) | Asynchronously write an attribute with a date value. |
| WriteAttributeAsync(TnefAttributeTag, Int32, CancellationToken) | Asynchronously write an attribute with an integer value. |
| WriteAttributeAsync(TnefAttributeTag, String, CancellationToken) | Asynchronously write an attribute with a string value. |
| WriteAttributeAsync(TnefAttributeTag, Byte, Int32, Int32, CancellationToken) | Asynchronously write an attribute with a raw value. |