Creating MIME-Formatted Messages

Creating MIME-Formatted Messages

By default, message body content is constructed using the Multipurpose Internet Mail Extension (MIME) encoding format. This operation is controlled using the IMessage.MIMEFormatted property, which defaults to True. There are many ways to create the MIME content hierarchy in a message. For the most common cases, such as adding an attachment or creating a MIME Encapsulation of Aggregate HTML Documents (MHTML) message, the IMessage interface exposes methods that automatically structure the MIME content and take care of the underlying details. These methods include IMessage.AddAttachment and IMessage.CreateMHTMLBody. To add attachments to a message in Uuencoded format, you set IMessage.MIMEFormatted to False.

In many cases, you may need to construct the entire body part hierarchy, or portions of it, manually.

Objects that are included in the body part hierarchy for a message expose implementations of the IBodyPart interface; for example, each instance of the BodyPart and Message Component Object Model (COM) classes represent one body part in the hierarchy of a message encoded in MIME. Both BodyPart and Message instances expose the IBodyPart interface to facilitate manipulation of the body part's content and properties. The IBodyPart.BodyParts collection contains the child BodyPart objects for the object and is therefore used to navigate down the hierarchy of objects. To navigate up the hierarchy from an object, you use each object's IBodyPart.Parent property.

The Message object acts as the root of the MIME hierarchy. In standard object-oriented fashion, the hierarchies extend in patterns of BodyPart -> BodyParts -> BodyPart until a terminating (leaf) node in the hierarchy is reached.

When using methods such as IMessage.CreateMHTMLBody or IMessage.AddAttachment, or the HTMLBody property on the message, the MIME hierarchy is constructed automatically. You do not need to manually add the BodyPart objects that contain the various content parts.

In many cases, you may want to manually create the BodyPart hierarchy. To do so, you can use the IBodyPart.AddBodyPart method to add child body parts to the object. Alternately, you can access the IBodyPart.BodyParts collection explicitly and use the IBodyParts.Add method to add the new body part. Both methods create a new BodyPart instance and return an object reference on the newly added object. Using this returned interface, you can then set any fields for the object using the IBodyPart.Fields collection. For body parts that hold content, you use the appropriate Stream object to populate the contents. You access the BodyPart object's Stream using either IBodyPart.GetEncodedContentStream or IBodyPart.GetDecodedContentStream.

See Also

Concepts

AddAttachment Method
CreateMHTMLBody Method
MimeFormatted Property
AddBodyPart Method
Add Method