Multipart Class

A multipart MIME entity which may contain a collection of MIME entities.

Definition

Namespace: MimeKit
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
C#
public class Multipart : MimeEntity, IMultipart, 
	IMimeEntity, IDisposable, ICollection<MimeEntity>, IEnumerable<MimeEntity>, 
	IEnumerable, IList<MimeEntity>
Inheritance
Object    MimeEntity    Multipart
Derived
Implements
IMimeEntity, IMultipart, ICollectionMimeEntity, IEnumerableMimeEntity, IListMimeEntity, IEnumerable, IDisposable

Remarks

All multipart MIME entities will have a Content-Type with a media type of "multipart". The most common multipart MIME entity used in email is the "multipart/mixed" entity.

Four (4) initial subtypes were defined in the original MIME specifications: mixed, alternative, digest, and parallel.

The "multipart/mixed" type is a sort of general-purpose container. When used in email, the first entity is typically the "body" of the message while additional entities are most often file attachments.

Speaking of message "bodies", the "multipart/alternative" type is used to offer a list of alternative formats for the main body of the message (usually they will be "text/plain" and "text/html"). These alternatives are in order of increasing faithfulness to the original document (in other words, the last entity will be in a format that, when rendered, will most closely match what the sending client's WYSISYG editor produced).

The "multipart/digest" type will typically contain a digest of MIME messages and is most commonly used by mailing-list software.

The "multipart/parallel" type contains entities that are all meant to be shown (or heard) in parallel.

Another commonly used type is the "multipart/related" type which contains, as one might expect, inter-related MIME parts which typically reference each other via URIs based on the Content-Id and/or Content-Location headers.

Example

C#
                            public static MimeMessage CreateMessageWithAttachment (string path)
        {
            var message = new MimeMessage ();
            message.From.Add (new MailboxAddress ("Joey", "joey@friends.com"));
            message.To.Add (new MailboxAddress ("Alice", "alice@wonderland.com"));
            message.Subject = "How you doin?";

            var body = new TextPart (TextFormat.Plain) {
                Text = @"Hey Alice,

What are you up to this weekend? Monica is throwing one of her parties on
Saturday and I was hoping you could make it.

Will you be my +1?

-- Joey
"
            };

            var attachment = new MimePart (MimeTypes.GetMimeType (path)) {
                Content = new MimeContent (File.OpenRead (path)),
                ContentDisposition = new ContentDisposition (ContentDisposition.Attachment),
                ContentTransferEncoding = ContentEncoding.Base64,
                FileName = Path.GetFileName (path)
            };

            // A multipart/mixed is a container for a list of independent MIME parts. The
            // message body is conventionally the first child, followed by the attachments.
            // A boundary string is generated automatically.
            var multipart = new Multipart ("mixed");
            multipart.Add (body);
            multipart.Add (attachment);

            message.Body = multipart;

            return message;
        }
                  
C#
                    public static void RemoveAttachments (MimeMessage message)
{
    if (message.Body is Multipart multipart)
        RemoveAttachments (multipart);
}

static void RemoveAttachments (Multipart multipart)
{
    // A Multipart is an IList<MimeEntity>, so its children can be added, removed or
    // replaced just like any other list. Iterate in reverse so that removing an item
    // does not affect the indexes of the children not yet visited.
    for (int i = multipart.Count - 1; i >= 0; i--) {
        var child = multipart[i];

        if (child is Multipart nested) {
            // Recurse into nested multiparts such as multipart/alternative.
            RemoveAttachments (nested);
        } else if (child.IsAttachment) {
            // Replace the attachment with a short note so that the recipient knows that
            // something was removed.
            var fileName = (child as MimePart)?.FileName ?? "attachment";

            multipart[i] = new TextPart (TextFormat.Plain) {
                Text = string.Format ("The attachment \"{0}\" has been removed.", fileName)
            };
        }
    }
}
                  

Constructors

Multipart Initialize a new instance of the Multipart class.
Multipart(MimeEntityConstructorArgs) Initialize a new instance of the Multipart class.
Multipart(String) Initialize a new instance of the Multipart class.
Multipart(String, Object) Initialize a new instance of the Multipart class.

Properties

Boundary Get or set the boundary.
ContentBase Get or set the base content URI.
(Inherited from MimeEntity)
ContentDisposition Get or set the content disposition.
(Inherited from MimeEntity)
ContentId Get or set the Content-Id.
(Inherited from MimeEntity)
ContentLocation Get or set the content location.
(Inherited from MimeEntity)
ContentType Get the type of the content.
(Inherited from MimeEntity)
Count Get the number of parts in the multipart.
Epilogue Get or set the epilogue.
Headers Get the list of headers.
(Inherited from MimeEntity)
IsAttachment Get a value indicating whether this MimePart is an attachment.
(Inherited from MimeEntity)
IsReadOnly Get a value indicating whether this instance is read only.
Item Get or set the MimeEntity at the specified index.
Preamble Get or set the preamble.

Methods

Accept Dispatches to the specific visit method for this MIME entity.
(Overrides MimeEntityAccept(MimeVisitor))
Add Add an entity to the multipart.
Clear Clear a multipart.
Clear(Boolean) Clear a multipart.
Contains Check if the Multipart contains the specified entity.
CopyTo Copy all the entities in the Multipart to the specified array.
Dispose Releases all resources used by the MimeEntity object.
(Inherited from MimeEntity)
Dispose(Boolean) Release the unmanaged resources used by the Multipart and optionally releases the managed resources.
(Overrides MimeEntityDispose(Boolean))
Equals Determines whether the specified object is equal to the current object.
(Inherited from Object)
Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object)
GetEnumerator Get the enumerator for the children of the Multipart.
GetHashCode Serves as the default hash function.
(Inherited from Object)
GetType Gets the Type of the current instance.
(Inherited from Object)
IndexOf Get the index of an entity.
Insert Insert an entity into the Multipart at the specified index.
MemberwiseClone Creates a shallow copy of the current Object.
(Inherited from Object)
OnHeadersChanged Called when the headers change in some way.
(Inherited from MimeEntity)
Prepare Prepare the MIME entity for transport using the specified encoding constraints.
(Overrides MimeEntityPrepare(EncodingConstraint, Int32))
Remove Remove an entity from the multipart.
RemoveAt Remove an entity from the Multipart at the specified index.
RemoveHeader Remove a header by name.
(Inherited from MimeEntity)
SetHeader(String, Byte) Set the value of a header using the raw value.
(Inherited from MimeEntity)
SetHeader(String, String) Set the value of a header.
(Inherited from MimeEntity)
ToString Return a String that represents the MimeEntity for debugging purposes.
(Inherited from MimeEntity)
TryGetValue Get the preferred message body if it exists.
TryInit Tries to use the given object to initialize the appropriate property.
(Inherited from MimeEntity)
WriteTo(Stream, CancellationToken) Write the MimeEntity to the specified output stream.
(Inherited from MimeEntity)
WriteTo(String, CancellationToken) Write the MimeEntity to the specified file.
(Inherited from MimeEntity)
WriteTo(FormatOptions, Stream, CancellationToken) Write the MimeEntity to the specified output stream.
(Inherited from MimeEntity)
WriteTo(FormatOptions, String, CancellationToken) Write the MimeEntity to the specified file.
(Inherited from MimeEntity)
WriteTo(Stream, Boolean, CancellationToken) Write the MimeEntity to the specified output stream.
(Inherited from MimeEntity)
WriteTo(String, Boolean, CancellationToken) Write the MimeEntity to the specified file.
(Inherited from MimeEntity)
WriteTo(FormatOptions, Stream, Boolean, CancellationToken) Write the MimeEntity to the specified output stream.
(Inherited from MimeEntity)
WriteTo(FormatOptions, String, Boolean, CancellationToken) Write the MimeEntity to the specified file.
(Inherited from MimeEntity)
WriteToAsync(Stream, CancellationToken) Asynchronously write the MimeEntity to the specified output stream.
(Inherited from MimeEntity)
WriteToAsync(String, CancellationToken) Asynchronously write the MimeEntity to the specified file.
(Inherited from MimeEntity)
WriteToAsync(FormatOptions, Stream, CancellationToken) Asynchronously write the MimeEntity to the specified output stream.
(Inherited from MimeEntity)
WriteToAsync(FormatOptions, String, CancellationToken) Asynchronously write the MimeEntity to the specified file.
(Inherited from MimeEntity)
WriteToAsync(Stream, Boolean, CancellationToken) Asynchronously write the MimeEntity to the specified output stream.
(Inherited from MimeEntity)
WriteToAsync(String, Boolean, CancellationToken) Asynchronously write the MimeEntity to the specified file.
(Inherited from MimeEntity)
WriteToAsync(FormatOptions, Stream, Boolean, CancellationToken) Asynchronously write the MimeEntity to the specified output stream.
(Inherited from MimeEntity)
WriteToAsync(FormatOptions, String, Boolean, CancellationToken) Asynchronously write the MimeEntity to the specified file.
(Inherited from MimeEntity)

See Also

Reference