Tnef Property Writer Class
Definition
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
public sealed class TnefPropertyWriter : IDisposable- Inheritance
- Object TnefPropertyWriter
- Implements
- IDisposable
Remarks
A TnefPropertyWriter writes the MAPI properties of a MapiProperties, Attachment or RecipientTable attribute. It is obtained by calling OpenPropertyWriter(TnefAttributeTag).
Each property is written by calling one of the WritePropertyTag methods followed by the property's values. A single-valued property must have exactly one value, except for a Null property which has none. A multi-valued property may have any number of values. Alternatively, a property that was read using a TnefPropertyReader may be copied using WriteProperty(TnefProperty).
For a RecipientTable attribute, each row of properties must be started by calling BeginRow.
The property and value counts, the value lengths and the padding are written automatically. The attribute is completed when the property writer is disposed. If the attribute is incomplete at that point (for example, because a single-valued property has no value), the attribute is discarded and the next call to the TnefWriter throws an InvalidOperationException.
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 ();
}
}
Properties
| PropertyCount | Get the number of properties. |
| RowCount | Get the number of rows. |
Methods
| BeginRow | Begin a new row of properties. |
| Dispose | Release all resources used by the TnefPropertyWriter 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) |
| GetType | Gets the Type of the current instance. (Inherited from Object) |
| OpenEmbeddedMessage | Open a writer for an embedded message. |
| OpenEmbeddedMessage(Int32) | Open a writer for an embedded message. |
| OpenRtfCompressedStream | Open a stream for writing a compressed RTF value. |
| OpenValueStream | Open a stream for writing a binary value. |
| ToString | Returns a string that represents the current object. (Inherited from Object) |
| WriteProperty | Write a property. |
| WritePropertyTag(TnefPropertyTag) | Begin a new property. |
| WritePropertyTag(TnefNameId, TnefPropertyType) | Begin a new named property. |
| WriteValue(Boolean) | Write a boolean value. |
| WriteValue(Byte) | Write a binary value. |
| WriteValue(DateTime) | Write a date and time value. |
| WriteValue(Decimal) | Write a currency value. |
| WriteValue(Double) | Write a double-precision floating-point value. |
| WriteValue(Guid) | Write a GUID value. |
| WriteValue(Int16) | Write a 16-bit integer value. |
| WriteValue(Int32) | Write a 32-bit integer value. |
| WriteValue(Int64) | Write a 64-bit integer value. |
| WriteValue(Single) | Write a single-precision floating-point value. |
| WriteValue(String) | Write a string value. |
| WriteValue(Byte, Int32, Int32) | Write a binary value. |