# MIME Component

The MIME component allows for the simple encoding and decoding of MIME structures, such as message attachments and file uploads.

## Syntax

```text
nsoftware.IPWorksSMIME.MIME
```

## Remarks

The component may be used for decoding or encoding of messages. [Message](#message-property-mime-component) holds the content of the encoded message or the filename that holds the encoded data. The decoded data are provided in [Parts](#parts-property-mime-component) collection.

To decode a MIME message, you should first assign either the whole message (headers and body) to [Message](#message-property-mime-component) or put the message headers into the [MessageHeaders](#messageheaders-property-mime-component) property and the message body or the filename holding it into the [Message](#message-property-mime-component) property. Calling [DecodeFromFile](#decodefromfile-method-mime-component) or [DecodeFromString](#decodefromstring-method-mime-component) directs the component to start parsing the message: it will fill out the [ContentType](#contenttype-property-mime-component) and [ContentTypeAttr](#contenttypeattr-property-mime-component) properties with message content-type information, and it will enter information about message parts into the [Parts](#parts-property-mime-component) collection, which includes part sizes, part content types, part content type attributes, part content disposition, part content disposition attributes, part encoding, part name, part filename, part headers, or part content (part-decoded string or part-decoded file) for each decoded part.

Calling the [DecodeFromString](#decodefromstring-method-mime-component) or [DecodeFromFile](#decodefromfile-method-mime-component) to decode does not actually decode the message but only parses it. The real decoding is done when the part content in the [Parts](#parts-property-mime-component) collection is first accessed.

To encode data into a MIME message, you should first assign values for each part to either a part-decoded string or a part- decoded file in the [Parts](#parts-property-mime-component) collection property, and optionally assign values to the other part attributes in the [Parts](#parts-property-mime-component) collection. Assigning a value to the part-decoded string or a part-decoded file through the [Parts](#parts-property-mime-component) collection, fills out automatically the part headers into the [Parts](#parts-property-mime-component) collection for the respective part. Calling [EncodeToFile](#encodetofile-method-mime-component) or [EncodeToString](#encodetostring-method-mime-component) directs the component to fill out the [Message](#message-property-mime-component) with the message body and the [MessageHeaders](#messageheaders-property-mime-component) with the headers.

The [Progress](#progress-event-mime-component) event is fired as the message is parsed and the data are decoded/encoded.

## Property List

*The following is the full list of the properties of the component with short descriptions. Click on the links for further details.*

|  |  |
| --- | --- |
| [Boundary](#boundary-property-mime-component) | This property includes the boundary separating the MIME parts. It has a maximum length of 80 bytes. |
| [ContentType](#contenttype-property-mime-component) | This property includes the value of the content-type header of the message that was encoded/decoded. |
| [ContentTypeAttr](#contenttypeattr-property-mime-component) | This property includes the attributes of the content-type header of the message that was encoded or decoded. |
| [Message](#message-property-mime-component) | This property contains the encoded message text or a path to a file that contains the encoded message text. |
| [MessageHeaders](#messageheaders-property-mime-component) | This property includes the headers of the MIME message. |
| [MessageHeadersString](#messageheadersstring-property-mime-component) | This property includes the string version of the MIME message headers. |
| [Parts](#parts-property-mime-component) | This property contains information about all parts of a MIME message. |

## Method List

*The following is the full list of the methods of the component with short descriptions. Click on the links for further details.*

|  |  |
| --- | --- |
| [Config](#config-method-mime-component) | Sets or retrieves a configuration setting. |
| [DecodeFromFile](#decodefromfile-method-mime-component) | This method decodes from a file. |
| [DecodeFromStream](#decodefromstream-method-mime-component) | This method decodes from a stream. |
| [DecodeFromString](#decodefromstring-method-mime-component) | This method decodes from a string. |
| [DecodePart](#decodepart-method-mime-component) | This method decodes the specified part. |
| [DecodePartToFile](#decodeparttofile-method-mime-component) | This method decodes the specified part to a file on disk. |
| [DecodePartToStream](#decodeparttostream-method-mime-component) | This method decodes the specified part to a stream. |
| [EncodeToFile](#encodetofile-method-mime-component) | This method encodes to a file. |
| [EncodeToStream](#encodetostream-method-mime-component) | This method encodes to a stream. |
| [EncodeToString](#encodetostring-method-mime-component) | This method encodes to a string. |
| [Reset](#reset-method-mime-component) | This method will reset the component. |
| [ResetData](#resetdata-method-mime-component) | This method resets the values of all headers and part properties. |

## Event List

*The following is the full list of the events fired by the component with short descriptions. Click on the links for further details.*

|  |  |
| --- | --- |
| [Error](#error-event-mime-component) | Fired when information is available about errors during data delivery. |
| [Header](#header-event-mime-component) | This event is fired every time a header is parsed. |
| [Progress](#progress-event-mime-component) | This event shows the progress of decoding or encoding the input data. |

## Config Settings

*The following is a list of config settings for the component with short descriptions. Click on the links for further details.*

|  |  |
| --- | --- |
| [AlternateCharsetNames](#AlternateCharsetNames) | Maps alternate charset names to the specified name. |
| [FoldHeaders](#FoldHeaders) | Tells the component whether to fold the headers. |
| [IncludeHeaders](#IncludeHeaders) | Tells the component whether to include the headers when encoding the message. |
| [MaxParts](#MaxParts) | The maximum number of parts to be parsed from a mime message. |
| [RequireVersionHeader](#RequireVersionHeader) | Specifies whether or not the component should require the version header. |
| [SanitizeFileName](#SanitizeFileName) | Whether invalid characters are replaced in MIME part filenames. |
| [TempFilePath](#TempFilePath) | If set, the temporary files created during MIME decoding and encoding will be put in the path specified. |
| [BuildInfo](#BuildInfo) | Information about the product's build. |
| [GUIAvailable](#GUIAvailable) | Whether or not a message loop is available for processing events. |
| [LicenseInfo](#LicenseInfo) | Information about the current license. |
| [MaskSensitiveData](#MaskSensitiveData) | Whether sensitive data is masked in log messages. |
| [UseInternalSecurityAPI](#UseInternalSecurityAPI) | Whether or not to use the system security libraries or an internal implementation. |

# Boundary Property ([MIME](#mime-component) Component)

This property includes the boundary separating the MIME parts. It has a maximum length of 80 bytes.

## Syntax

```text
public string Boundary { get; set; }
```

## Default Value

""

## Remarks

This property contains the boundary separating the MIME parts and has a maximum length of 80 bytes. The component automatically generates a random boundary value during encoding, if no value is given. During decoding, this property is filled with the boundary from the actual MIME message.

The component will truncate any string longer than 80 bytes if assigned to Boundary.

# ContentType Property ([MIME](#mime-component) Component)

This property includes the value of the content-type header of the message that was encoded/decoded.

## Syntax

```text
public string ContentType { get; set; }
```

## Default Value

""

## Remarks

This property contains the value of the content-type header of the message that was encoded/decoded. The component filters it out from the [MessageHeaders](#messageheaders-property-mime-component). It shows the user information about the type of the parts and their relation to each other.

The content-type attributes, such as filename, boundary, and charset, are held in this property.

# ContentTypeAttr Property ([MIME](#mime-component) Component)

This property includes the attributes of the content-type header of the message that was encoded or decoded.

## Syntax

```text
public string ContentTypeAttr { get; set; }
```

## Default Value

""

## Remarks

This property contains content-type attributes such as "filename", "boundary", and "charset".

# Message Property ([MIME](#mime-component) Component)

This property contains the encoded message text or a path to a file that contains the encoded message text.

## Syntax

```text
public string Message { get; set; }
public byte[] MessageB { get; set; }
```

## Default Value

""

## Remarks

This property contains the encoded message text or a path to a file that contains the encoded message text. The component fills out this property with the message body and [MessageHeaders](#messageheaders-property-mime-component) during encoding. The user may assign both the headers and the body to this property and leave [MessageHeaders](#messageheaders-property-mime-component) empty before decoding.

This property is not available at design time.

# MessageHeaders Property ([MIME](#mime-component) Component)

This property includes the headers of the MIME message.

## Syntax

```text
public HeaderList MessageHeaders { get; }
```

## Remarks

This property contains the headers of the MIME message. The component fills out this property upon encoding. The user should use them as additional headers when emailing the [Message](#message-property-mime-component).

During decoding, if this property is empty, the component will try to find the headers in the beginning of the [Message](#message-property-mime-component) and then will fill out this property accordingly.

 Please refer to the [Header](#header-type) type for a complete list of fields.

# MessageHeadersString Property ([MIME](#mime-component) Component)

This property includes the string version of the MIME message headers.

## Syntax

```text
public string MessageHeadersString { get; set; }
```

## Default Value

""

## Remarks

The component fills out MessageHeadersString upon encoding. The user should use these strings as additional headers when emailing the [Message](#message-property-mime-component).

This property contains a string version of the MIME message headers. During decoding, if this property is empty, the component will try to find the headers in the beginning of the [Message](#message-property-mime-component) and will fill out this property accordingly.

# Parts Property ([MIME](#mime-component) Component)

This property contains information about all parts of a MIME message.

## Syntax

```text
public MIMEPartList Parts { get; }
```

## Remarks

This property contains information about all parts of a MIME message. A MIME message consists of different parts, separated by either a user-specified or an auto-generated boundary. Each part has its own set of headers that are contained in the [Headers](#MIMEPart_f_Headers) field.

The MIMEPARTS collection is filled out by the component when a call to either [DecodeFromFile](#decodefromfile-method-mime-component) or [DecodeFromString](#decodefromstring-method-mime-component) is made. However, when the [EncodeToFile](#encodetofile-method-mime-component) or [EncodeToString](#encodetostring-method-mime-component) methods are called, the component encodes the data given in either [DecodedFile](#MIMEPart_f_DecodedFile) or [DecodedString](#MIMEPart_f_DecodedString) into a MIME message.

This collection is indexed from *0* to *Count - 1*.

 Please refer to the [MIMEPart](#mimepart-type) type for a complete list of fields.

# Config Method ([MIME](#mime-component) Component)

Sets or retrieves a configuration setting.

## Syntax

```text
public string Config(string configurationString);

Async Version
public async Task<string> Config(string configurationString);
public async Task<string> Config(string configurationString, CancellationToken cancellationToken);
```

## Remarks

Config is a generic method available in every component. It is used to set and retrieve [configuration settings](#config-settings-mime-component) for the component.

These settings are similar in functionality to properties, but they are rarely used. In order to avoid "polluting" the property namespace of the component, access to these *internal properties* is provided through the Config method.

To set a configuration setting named *PROPERTY*, you must call *Config("PROPERTY=VALUE")*, where *VALUE* is the value of the setting expressed as a string. For boolean values, use the strings "True", "False", "0", "1", "Yes", or "No" (case does not matter).

To read (query) the value of a [configuration setting](#config-settings-mime-component), you must call *Config("PROPERTY")*. The value will be returned as a string.

# DecodeFromFile Method ([MIME](#mime-component) Component)

This method decodes from a file.

## Syntax

```text
public void DecodeFromFile();

Async Version
public async Task DecodeFromFile();
public async Task DecodeFromFile(CancellationToken cancellationToken);
```

## Remarks

This method decodes the data file given in [Message](#message-property-mime-component) and optionally [MessageHeaders](#messageheaders-property-mime-component) and fills out the [Parts](#parts-property-mime-component) collection for each decoded part.

**Example. Decoding from a File:**

```text
MIMEControl.Message = "C:\filename.txt"
MIMEControl.DecodeFromFile
```

In this example, *C:\filename.txt* must contain the MIME headers and body. If not, the [MessageHeaders](#messageheaders-property-mime-component) property must be set to the appropriate MIME headers.

# DecodeFromStream Method ([MIME](#mime-component) Component)

This method decodes from a stream.

## Syntax

```text
public void DecodeFromStream(System.IO.Stream inputStream);

Async Version
public async Task DecodeFromStream(System.IO.Stream inputStream);
public async Task DecodeFromStream(System.IO.Stream inputStream, CancellationToken cancellationToken);
```

## Remarks

This method decodes from a stream. It is the same as DecodeFromString, but now the MIME message is passed to this method as a stream.

This method decodes the data given by the *InputStream* and optionally [MessageHeaders](#messageheaders-property-mime-component) and fills out the [Parts](#parts-property-mime-component) collection.

**Example. Decoding a Mail Message:**

```text
MIMEControl.MessageHeaders.AddRange(MailControl.MessageHeaders)
MIMEControl.DecodeFromStream(InputStream)
```

# DecodeFromString Method ([MIME](#mime-component) Component)

This method decodes from a string.

## Syntax

```text
public void DecodeFromString();

Async Version
public async Task DecodeFromString();
public async Task DecodeFromString(CancellationToken cancellationToken);
```

## Remarks

This method decodes from a string. It is the same as DecodeFromFile, but now the [Message](#message-property-mime-component) denotes the real encoded message rather than a filename.

This method decodes the data given in [Message](#message-property-mime-component) and optionally [MessageHeaders](#messageheaders-property-mime-component) and fills out the [Parts](#parts-property-mime-component) collection.

**Example. Decoding a Mail Message:**

```text
MIMEControl.MessageHeaders.AddRange(MailControl.MessageHeaders)
MIMEControl.Message = MailControl.MessageText
MIMEControl.DecodeFromString
```

# DecodePart Method ([MIME](#mime-component) Component)

This method decodes the specified part.

## Syntax

```text
public byte[] DecodePart(int index);

Async Version
public async Task<byte[]> DecodePart(int index);
public async Task<byte[]> DecodePart(int index, CancellationToken cancellationToken);
```

## Remarks

This method decodes the part specified by *index* and returns the decoded part data.

[DecodeFromString](#decodefromstring-method-mime-component) or [DecodeFromFile](#decodefromfile-method-mime-component) must be called before calling this method. Calling this method will return the decoded bytes, and in addition, the [DecodedString](#MIMEPart_f_DecodedString) field will be populated with the string representation of the decoded data.

**Example. Decoding a Specified Part:**

```csharp
mime.MessageHeadersString = myMessageHeaders;
mime.Message = myMessageBody;
mime.DecodeFromString();

byte[] myPartContent = await mime.DecodePart(0);
```

# DecodePartToFile Method ([MIME](#mime-component) Component)

This method decodes the specified part to a file on disk.

## Syntax

```text
public string DecodePartToFile(int index);

Async Version
public async Task<string> DecodePartToFile(int index);
public async Task<string> DecodePartToFile(int index, CancellationToken cancellationToken);
```

## Remarks

This method decodes the part specified by *index* to a temporary file. The full path to the temporary file is returned by this method. The user is responsible for deleting the temporary file.

[DecodeFromString](#decodefromstring-method-mime-component) or [DecodeFromFile](#decodefromfile-method-mime-component) must be called before calling this method.

# DecodePartToStream Method ([MIME](#mime-component) Component)

This method decodes the specified part to a stream.

## Syntax

```text
public void DecodePartToStream(int index, System.IO.Stream outputStream);

Async Version
public async Task DecodePartToStream(int index, System.IO.Stream outputStream);
public async Task DecodePartToStream(int index, System.IO.Stream outputStream, CancellationToken cancellationToken);
```

## Remarks

This method decodes the part specified by *index* to a stream specified by *OutputStream*.

[DecodeFromString](#decodefromstring-method-mime-component) or [DecodeFromFile](#decodefromfile-method-mime-component) must be called before calling this method.

# EncodeToFile Method ([MIME](#mime-component) Component)

This method encodes to a file.

## Syntax

```text
public void EncodeToFile();

Async Version
public async Task EncodeToFile();
public async Task EncodeToFile(CancellationToken cancellationToken);
```

## Remarks

This method encodes the data given in [Parts](#parts-property-mime-component) collection (either a decoded file or a decoded string) into a MIME message at [Message](#message-property-mime-component) and [MessageHeaders](#messageheaders-property-mime-component) using the values optionally given in [Parts](#parts-property-mime-component) collection. To encode several objects into a MIME envelope, the user should first clear any old values left in the [Parts](#parts-property-mime-component) collection.

The component will check for each item in [Parts](#parts-property-mime-component) collection (first the decoded file and then the decoded string) for the data to encode.

The encoded message will be saved in the file pointed to by [Message](#message-property-mime-component); the message headers will be held by [MessageHeaders](#messageheaders-property-mime-component).

**Example. Encoding Data:**

```text
MIMEControl.ResetData()
MIMEControl.Parts.Add(new MIMEPart())
MIMEControl.Parts.Add(new MIMEPart())

MIMEControl.Parts[0].DecodedString = "This is plain text data"
MIMEControl.Parts[0].Encoding = peQuotedPrintable
MIMEControl.Parts[0].ContentType = "text/plain"
MIMEControl.Parts[1].DecodedFile = "C:\filename.txt"

MIMEControl.Message = "temp.tmp"
MIMEControl.EncodeToFile()
```

# EncodeToStream Method ([MIME](#mime-component) Component)

This method encodes to a stream.

## Syntax

```text
public void EncodeToStream(System.IO.Stream outputStream);

Async Version
public async Task EncodeToStream(System.IO.Stream outputStream);
public async Task EncodeToStream(System.IO.Stream outputStream, CancellationToken cancellationToken);
```

## Remarks

This method encodes to a stream. It is the same as [EncodeToString](#encodetostring-method-mime-component), but the encoded data are written to a stream instead of to the [Message](#message-property-mime-component) property.

This method encodes the data given in [Parts](#parts-property-mime-component) collection into a MIME message. The Message gets written to the specified *OutputStream* and [MessageHeaders](#messageheaders-property-mime-component) using the values optionally given in the [Parts](#parts-property-mime-component) collection. To encode several objects into a MIME envelope, the user should first clear any old values left in the [Parts](#parts-property-mime-component) collection by calling [ResetData](#resetdata-method-mime-component).

The component will check each item in the [Parts](#parts-property-mime-component) collection (first the decoded file and then the decoded string) for the data to encode.

The encoded message will be written to the specified *OutputStream*, and the message headers will be held by [MessageHeaders](#messageheaders-property-mime-component).

**Example. Encoding Data:**

```text
MIMEControl.ResetData()
MIMEControl.Parts.Add(new MIMEPart())
MIMEControl.Parts.Add(new MIMEPart())

MIMEControl.Parts[0].DecodedString = "This is plain text data"
MIMEControl.Parts[0].Encoding = peQuotedPrintable
MIMEControl.Parts[0].ContentType = "text/plain"
MIMEControl.Parts[1].DecodedFile = "C:\filename.txt"

MIMEControl.EncodeToStream(OutputStream)
```

# EncodeToString Method ([MIME](#mime-component) Component)

This method encodes to a string.

## Syntax

```text
public void EncodeToString();

Async Version
public async Task EncodeToString();
public async Task EncodeToString(CancellationToken cancellationToken);
```

## Remarks

This method encodes to a string. It is the same as [EncodeToFile](#encodetofile-method-mime-component) but the encoded data are held in the [Message](#message-property-mime-component) property rather than saved to a file.

This method encodes the data given in the [Parts](#parts-property-mime-component) collection into a MIME message. The message gets stored in [Message](#message-property-mime-component) and [MessageHeaders](#messageheaders-property-mime-component) using the values optionally given in [Parts](#parts-property-mime-component) collection. To encode several objects into a MIME envelope, the user should first clear any old values left in the [Parts](#parts-property-mime-component) collection by calling [ResetData](#resetdata-method-mime-component).

The component will check each item in the [Parts](#parts-property-mime-component) collection (first the decoded file and then the decoded string) for the data to encode.

The encoded message will be stored in the [Message](#message-property-mime-component) property; the message headers will be held by [MessageHeaders](#messageheaders-property-mime-component).

**Example. Encoding Data:**

```text
MIMEControl.ResetData()
MIMEControl.Parts.Add(new MIMEPart())
MIMEControl.Parts.Add(new MIMEPart())

MIMEControl.Parts[0].DecodedString = "This is plain text data"
MIMEControl.Parts[0].Encoding = peQuotedPrintable
MIMEControl.Parts[0].ContentType = "text/plain"
MIMEControl.Parts[1].DecodedFile = "C:\filename.txt"

MIMEControl.EncodeToString()
```

# Reset Method ([MIME](#mime-component) Component)

This method will reset the component.

## Syntax

```text
public void Reset();

Async Version
public async Task Reset();
public async Task Reset(CancellationToken cancellationToken);
```

## Remarks

This method will reset the component's properties to their default values.

# ResetData Method ([MIME](#mime-component) Component)

This method resets the values of all headers and part properties.

## Syntax

```text
public void ResetData();

Async Version
public async Task ResetData();
public async Task ResetData(CancellationToken cancellationToken);
```

## Remarks

This method resets the values of all headers and part properties. It is an easy way to reset the component's properties before starting to populate the part properties with new values.

# Error Event ([MIME](#mime-component) Component)

Fired when information is available about errors during data delivery.

## Syntax

```text
public event OnErrorHandler OnError;

public delegate void OnErrorHandler(object sender, MIMEErrorEventArgs e);

public class MIMEErrorEventArgs : EventArgs {
  public int ErrorCode { get; }
  public string Description { get; }
}
```

## Remarks

The Error event is fired in case of exceptional conditions during message processing. Normally the component throws an exception.

The *ErrorCode* parameter contains an error code, and the *Description* parameter contains a textual description of the error. For a list of valid error codes and their descriptions, please refer to the [Error Codes](#trappable-errors-mime-component) section.

# Header Event ([MIME](#mime-component) Component)

This event is fired every time a header is parsed.

## Syntax

```text
public event OnHeaderHandler OnHeader;

public delegate void OnHeaderHandler(object sender, MIMEHeaderEventArgs e);

public class MIMEHeaderEventArgs : EventArgs {
  public int PartIndex { get; }
  public string Field { get; }
  public string Value { get; }  public byte[] ValueB { get; }
}
```

## Remarks

The *PartIndex* parameter contains the index of the part containing the current header. If the header is from the MIME body, this value will be -1. The *Field* parameter contains the name of the MIME header. The *Value* parameter contains the header contents. The Header event is fired when a header is parsed, which occurs for each header of the message when [DecodeFromFile](#decodefromfile-method-mime-component), [DecodeFromString](#decodefromstring-method-mime-component), or [DecodeFromStream](#decodefromstream-method-mime-component) is called.

# Progress Event ([MIME](#mime-component) Component)

This event shows the progress of decoding or encoding the input data.

## Syntax

```text
public event OnProgressHandler OnProgress;

public delegate void OnProgressHandler(object sender, MIMEProgressEventArgs e);

public class MIMEProgressEventArgs : EventArgs {
  public int PercentDone { get; }
}
```

## Remarks

The Progress allows the user to visualize the progress of processing the input data.

The *PercentDone* parameter shows what percentage of the input has been read.

# Header Type

This is an HTTP header as it is received from the server.

## Remarks

When a header is received through a Header event, it is parsed into a [Header](#header-type) type. This type contains a [Field](#Header_f_Field), and its corresponding [Value](#Header_f_Value).

The following fields are available:

- [Field](#Header_f_Field)

- [Value](#Header_f_Value)

## Fields

 **Field** *string*
Default: ""

This field contains the name of the HTTP [Header](#header-type) (this is the same case as it is delivered).

 **Value** *string*
Default: ""

This field contains the [Header](#header-type) contents.

## Constructors

```text
public Header();
```

```text
public Header(string field, string value);
```

# MIMEPart Type

This is a part of the MIME message.

## Remarks

Each MIME message is split into parts. A message must have at least one part to be a valid MIME message. The message itself is split into the [MessageHeaders](#messageheaders-property-mime-component) and the [Message](#message-property-mime-component) body. The body is then split into MIME parts by a specified or generated [Boundary](#boundary-property-mime-component). Each part of a MIME message has its own [Headers](#MIMEPart_f_Headers) as well. These headers describe the [MIMEPart](#mimepart-type) and its contents.

The decoded data will be held in [DecodedString](#MIMEPart_f_DecodedString) or otherwise will be saved in a temporary file given by [DecodedFile](#MIMEPart_f_DecodedFile) when the user asks for the value of the respective field.

If the encoded data are file data, the decoded file(s) will be given in the [DecodedFile](#MIMEPart_f_DecodedFile) field. Otherwise, the decoded data will be in the [DecodedString](#MIMEPart_f_DecodedString) field.

When the [EncodeToString](#encodetostring-method-mime-component) method is called, the component will check for each item in the [Parts](#parts-property-mime-component) (first the [DecodedFile](#MIMEPart_f_DecodedFile) and then the [DecodedString](#MIMEPart_f_DecodedString)) for the data to encode.

The following fields are available:

- [ContentDisposition](#MIMEPart_f_ContentDisposition)

- [ContentDispositionAttr](#MIMEPart_f_ContentDispositionAttr)

- [ContentId](#MIMEPart_f_ContentId)

- [ContentType](#MIMEPart_f_ContentType)

- [ContentTypeAttr](#MIMEPart_f_ContentTypeAttr)

- [DecodedFile](#MIMEPart_f_DecodedFile)

- [DecodedString](#MIMEPart_f_DecodedString)

- [Encoding](#MIMEPart_f_Encoding)

- [FileName](#MIMEPart_f_FileName)

- [Headers](#MIMEPart_f_Headers)

- [Name](#MIMEPart_f_Name)

- [PartInputStream](#MIMEPart_f_PartInputStream)

- [Size](#MIMEPart_f_Size)

## Fields

 **ContentDisposition** *string*
Default: ""

This field contains content disposition for each part.

The value in this field is used in the *Content-Disposition* header. Typical values include "form-data" and "attachment". The component fills it out each time the [DecodedFile](#MIMEPart_f_DecodedFile) is changed. Changing the [ContentDisposition](#MIMEPart_f_ContentDisposition) recalculates the [Headers](#MIMEPart_f_Headers) as well.

 **ContentDispositionAttr** *string*
Default: ""

This field contains the content disposition's attribute (if any) for each part.

Typical values for this field are the names of the form-variables used while creating an HTTP post or the values for filenames.

Setting this field recalculates the [Headers](#MIMEPart_f_Headers).

 **ContentId** *string*
Default: ""

This field contains the content identifier for each part. The value of this field is used in the *Content-Id* header. The component fills it out each time the [DecodedFile](#MIMEPart_f_DecodedFile) is changed. Changing this field recalculates the [Headers](#MIMEPart_f_Headers) as well.

 **ContentType** *string*
Default: ""

This field contains the content type for each part. The value of this field is used in the *Content-Type* header. Typical values include "image/gif" and "text/plain". The component fills it out each time the [DecodedFile](#MIMEPart_f_DecodedFile) is changed. Changing this field recalculates the [Headers](#MIMEPart_f_Headers) as well.

 **ContentTypeAttr** *string*
Default: ""

This field contains the content-type attribute, if any, for each part. Setting this recalculates the [Headers](#MIMEPart_f_Headers) as well.

 **DecodedFile** *string*
Default: ""

This field may be set to a file on disk before calling [EncodeToString](#encodetostring-method-mime-component), [EncodeToFile](#encodetofile-method-mime-component) or [EncodeToStream](#encodetostream-method-mime-component). When encoding, data for the part will be read from the specified file.

When decoding, call [DecodePartToFile](#decodeparttofile-method-mime-component) to decode a specific part to a file on disk.

 **DecodedString** *string*
Default: ""

This field holds the actual content of each part. The component decodes the actual part of [Message](#message-property-mime-component) to [DecodedString](#MIMEPart_f_DecodedString) when [DecodedString](#MIMEPart_f_DecodedString)'s value is first queried.

Setting a value to [DecodedString](#MIMEPart_f_DecodedString) fills out the corresponding [Size](#MIMEPart_f_Size) with the string size if the corresponding [DecodedFile](#MIMEPart_f_DecodedFile) is empty.

The component checks during encoding first [DecodedFile](#MIMEPart_f_DecodedFile): if it is empty, then the component uses the value of [DecodedString](#MIMEPart_f_DecodedString).

NOTE: It is recommended to use the [DecodePart](#decodepart-method-mime-component) method instead of querying this property.

 **DecodedStringB** *byte []*
Default: ""

This field holds the actual content of each part. The component decodes the actual part of [Message](#message-property-mime-component) to [DecodedString](#MIMEPart_f_DecodedString) when [DecodedString](#MIMEPart_f_DecodedString)'s value is first queried.

Setting a value to [DecodedString](#MIMEPart_f_DecodedString) fills out the corresponding [Size](#MIMEPart_f_Size) with the string size if the corresponding [DecodedFile](#MIMEPart_f_DecodedFile) is empty.

The component checks during encoding first [DecodedFile](#MIMEPart_f_DecodedFile): if it is empty, then the component uses the value of [DecodedString](#MIMEPart_f_DecodedString).

NOTE: It is recommended to use the [DecodePart](#decodepart-method-mime-component) method instead of querying this property.

 **Encoding** *MIMEPartEncodings*
Default: 0

This field contains the actual content encoding type for each part. This field determines how to encode the data or how they actually were encoded in the [Message](#message-property-mime-component), as specified in the *Content-Transfer-Encoding* header.

Possible values for the this field are as follows:

|  |  |
| --- | --- |
| pe7bit (0) | 7 Bit data, no encoding. |
| peQuotedPrintable (1) | Quoted-Printable encoding of (typically) text. |
| peBase64 (2) | Base64 encoding of binary data. |
| pe8Bit (3) | No encoding, 8-bit characters may be contained as well. |
| peBinary (4) | Binary data without any encoding. Similar to 3 (8-bit encoding). |
| peUUEncode (5) | Uuencoding of binary data. |

 **FileName** *string*
Default: ""

This field contains the filename attribute specified in the headers of the part. Changing the value of the corresponding [DecodedFile](#MIMEPart_f_DecodedFile) automatically sets the [FileName](#MIMEPart_f_FileName) and consequently the [Headers](#MIMEPart_f_Headers).

 **Headers** *string*
Default: ""

This field contains the headers for each MIME part. The component fills out this field each time any of the other corresponding part properties are changed. If additional headers are needed, they should be appended after all of the other corresponding part properties are set.

 **Name** *string*
Default: ""

This field contains the name given to a part, such as the filename. Changing the value of [DecodedFile](#MIMEPart_f_DecodedFile) automatically sets the corresponding [Name](#MIMEPart_f_Name) and consequently the [Headers](#MIMEPart_f_Headers).

 **PartInputStream** *System.IO.Stream*
Default: ""

This field contains a stream that contains the actual content of each part before the message is encoded.

 **Size** *int (read-only)*
Default: 0

This field contains the size of the [DecodedFile](#MIMEPart_f_DecodedFile) or [DecodedString](#MIMEPart_f_DecodedString). If a non-empty value is assigned to [DecodedFile](#MIMEPart_f_DecodedFile), then component fills out the corresponding [Size](#MIMEPart_f_Size) with the file size; if the file does not exist, an error occurs.

If the [DecodedFile](#MIMEPart_f_DecodedFile) is empty and a value is assigned to [DecodedString](#MIMEPart_f_DecodedString), then the corresponding [Size](#MIMEPart_f_Size) will be equal to the string's size.

During decoding, this field is filled with the size of that part.

## Constructors

```text
public MIMEPart();
```

```text
public MIMEPart(string decodedFile);
```

```text
public MIMEPart(byte[] decodedString);
```

```text
public MIMEPart(System.IO.Stream partInputStream);
```

```text
public MIMEPart(System.IO.Stream partInputStream, string encoding);
```

# Config Settings ([MIME](#mime-component) Component)

 The component accepts one or more of the following *configuration settings*. Configuration settings are similar in functionality to properties, but they are rarely used. In order to avoid "polluting" the property namespace of the component, access to these *internal properties* is provided through the [Config](#config-method-mime-component) method.

### MIME Config Settings

**AlternateCharsetNames**: Maps alternate charset names to the specified name.Use this configuration setting to map alternate charset names to a valid charset name. The mappings are set in the following format:

*AlternateCharsetNames=altName1:validName1;altName2:validname2*

The *altName* parameters are case-insensitive to allow for a greater number of alternate names to be handled. The *validName* parameters are case-sensitive to ensure that the correct charset is used. Mappings are done on a one-to-one basis. If there are several alternate names that are not covered by case-insensitivity, multiple mappings will need to be used. For example, Example 1 showcases setting up to alternate names for the *cp850* charset. Example 2 however, is redundant since *cp1252* and *CP1252* are equivalent alternate names.

Example 1: cp850

```text
mime.Config("AlternateCharsetNames=cp-850:cp850;_cp_850:cp850");
```

Example 2: windows-1252

```text
mime.Config("AlternateCharsetNames=cp1252:windows-1252;CP1252:windows-1252");
```

**FoldHeaders**: Tells the component whether to fold the headers.If True, the component will fold the headers if the headers are over a certain length. If False, the headers will be on one line regardless of length.

**IncludeHeaders**: Tells the component whether to include the headers when encoding the message.If True, the component will include the headers when [EncodeToFile](#encodetofile-method-mime-component) or [EncodeToString](#encodetostring-method-mime-component) are called. If False, only the message will be encoded.

The default value for [IncludeHeaders](#IncludeHeaders) is False.

**MaxParts**: The maximum number of parts to be parsed from a mime message.This configuration setting should be set to limit the amount of parts that will be parsed by the component and saved into the collection. By default, this value is 100. Therefore, up to 100 parts will be parsed from a mime message by default.

**RequireVersionHeader**: Specifies whether or not the component should require the version header.If True, the component will require that the "MIME-Version" header be included in the [MessageHeaders](#messageheaders-property-mime-component). If the header is not present during decoding, the component throws an exception. This configuration setting is False by default.

**SanitizeFileName**: Whether invalid characters are replaced in MIME part filenames.If True, the component will replace invalid characters with an underscore when decoding the MIME message. This applies to the filename held in the [FileName](#MIMEPart_f_FileName) field. When True, the following characters are considered invalid:

- \
- /
- "
- :
- ?
- *
- <
- >
- |

 Any characters with ASCII values between 0 and 31 are also considered invalid. The default value is False.

**TempFilePath**: If set, the temporary files created during MIME decoding and encoding will be put in the path specified. The [TempFilePath](#TempFilePath) property sets the path at which the temporary files will be created.

### Base Config Settings

**BuildInfo**: Information about the product's build.When queried, this setting will return a string containing information about the product's build.

**GUIAvailable**: Whether or not a message loop is available for processing events.In a GUI-based application, long-running blocking operations may cause the application to stop responding to input until the operation returns. The component will attempt to discover whether or not the application has a message loop and, if one is discovered, it will process events in that message loop during any such blocking operation.

In some non-GUI applications, an invalid message loop may be discovered that will result in errant behavior. In these cases, setting [GUIAvailable](#GUIAvailable) to *false* will ensure that the component does not attempt to process external events.

**LicenseInfo**: Information about the current license.When queried, this setting will return a string containing information about the license this instance of a component is using. It will return the following information:

- Product: The product the license is for.
- Product Key: The key the license was generated from.
- License Source: Where the license was found (e.g., RuntimeLicense, License File).
- License Type: The type of license installed (e.g., Royalty Free, Single Server).
- Last Valid Build: The last valid build number for which the license will work.

**MaskSensitiveData**: Whether sensitive data is masked in log messages.In certain circumstances it may be beneficial to mask sensitive data, like passwords, in log messages. Set this to *true* to mask sensitive data. The default is *true*.

**UseInternalSecurityAPI**: Whether or not to use the system security libraries or an internal implementation. When set to *false*, the component will use the system security libraries by default to perform cryptographic functions where applicable. In this case, calls to unmanaged code will be made. In certain environments, this is not desirable. To use a completely managed security implementation, set this setting to *true*.

Setting this configuration setting to *true* tells the component to use the internal implementation instead of using the system security libraries.

 On Windows, this setting is set to *false* by default. On Linux/macOS, this setting is set to *true* by default.

NOTE: This setting is static. The value set is applicable to all components used in the application.

When this value is set, the product's system dynamic link library (DLL) is no longer required as a reference, as all unmanaged code is stored in that file.

# Trappable Errors ([MIME](#mime-component) Component)

### MIME Errors

|  |  |
| --- | --- |
| 3 | Can't create the file for write (illegal name or disk is write-protected). |
| 4 | Can't open the file for read (doesn't exist?). |
| 5 | Can't read from file. |
| 6 | Can't write to file (disk full?). |
| 280 | Invalid Part Index. |
| 281 | Unknown MIME type. |
| 282 | No MIME-boundary found. |
| 283 | No file given. |
| 284 | The component is busy. |
| 285 | Can't create a temporary file to decode the data. |
| 286 | Can't read [Message](#message-property-mime-component) file. |
| 287 | No header separator found. |
| 289 | No separator found. |
| 290 | Input stream must have seeking enabled. |
