MimePartFileName Property

Get or set the name of the file.

Definition

Namespace: MimeKit
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
C#
public string? FileName { get; set; }

Property Value

String
The name of the file.

Implements

IMimePartFileName

Remarks

First checks for the "filename" parameter on the Content-Disposition header. If that does not exist, then the "name" parameter on the Content-Type header is used.

When setting the filename, both the "filename" parameter on the Content-Disposition header and the "name" parameter on the Content-Type header are set.

  Caution

The filename, by its nature of being provided by an untrusted source, be used with caution. It is recommended that this value be sanitized before use.

Example

C#
                    foreach (var attachment in message.Attachments) {
    if (attachment is MessagePart) {
        var fileName = attachment.ContentDisposition?.FileName ?? attachment.ContentType.Name;
        var rfc822 = (MessagePart) attachment;

        if (string.IsNullOrEmpty (fileName))
            fileName = "attached-message.eml";

        // make sure that the filename value does not contain a full path or invalid path characters
        fileName = Path.GetFileName (fileName);

        using (var stream = File.Create (fileName))
            rfc822.Message.WriteTo (stream);
    } else {
        var part = (MimePart) attachment;
        var fileName = part.FileName;

        if (string.IsNullOrEmpty (fileName))
            fileName = "untitled.dat";

        // make sure that the filename value does not contain a full path or invalid path characters
        fileName = Path.GetFileName (fileName);

        using (var stream = File.Create (fileName))
            part.Content.DecodeTo (stream);
    }
}
                  

Exceptions

ObjectDisposedException The MimePart has been disposed.

See Also