[[mime-multipart-dataformat]]
= MIME Multipart DataFormat

*Available as of Camel version 2.17*

This data format that can convert a Camel message with attachments into
a Camel message having a MIME-Multipart message as message body (and no
attachments).

The use case for this is to enable the user to send attachments over
endpoints that do not directly support attachments, either as special
protocol implementation (e.g. send a MIME-multipart over an HTTP
endpoint) or as a kind of tunneling solution (e.g. because camel-jms
does not support attachments but by marshalling the message with
attachments into a MIME-Multipart, sending that to a JMS queue,
receiving the message from the JMS queue and unmarshalling it again
(into a message body with attachments).

The marshal option of the mime-multipart data format will convert a
message with attachments into a MIME-Multipart message. If the parameter
"multipartWithoutAttachment" is set to true it will also marshal
messages without attachments into a multipart message with a single
part, if the parameter is set to false it will leave the message alone.

MIME headers of the mulitpart as "MIME-Version" and "Content-Type" are
set as camel headers to the message. If the parameter "headersInline" is
set to true it will also create a MIME multipart message in any case.  +
Furthermore the MIME headers of the multipart are written as part of the
message body, not as camel headers.

The unmarshal option of the mime-multipart data format will convert a
MIME-Multipart message into a camel message with attachments and leaves
other messages alone. MIME-Headers of the MIME-Multipart message have to
be set as Camel headers. The unmarshalling will only take place if the
"Content-Type" header is set to a "multipart" type. If the option
"headersInline" is set to true, the body is always parsed as a MIME
message.As a consequence if the message body is a stream and stream
caching is not enabled, a message body that is actually not a MIME
message with MIME headers in the message body will be replaced by an
empty message. Up to Camel version 2.17.1 this will happen all message
bodies that do not contain a MIME multipart message regardless of body
type and stream cache setting.

== Options

// dataformat options: START
The MIME Multipart dataformat supports 6 options, which are listed below.



[width="100%",cols="2s,1m,1m,6",options="header"]
|===
| Name | Default | Java Type | Description
| multipartSubType | mixed | String | Specify the subtype of the MIME Multipart. Default is mixed.
| multipartWithoutAttachment | false | Boolean | Defines whether a message without attachment is also marshaled into a MIME Multipart (with only one body part). Default is false.
| headersInline | false | Boolean | Defines whether the MIME-Multipart headers are part of the message body (true) or are set as Camel headers (false). Default is false.
| includeHeaders |  | String | A regex that defines which Camel headers are also included as MIME headers into the MIME multipart. This will only work if headersInline is set to true. Default is to include no headers
| binaryContent | false | Boolean | Defines whether the content of binary parts in the MIME multipart is binary (true) or Base-64 encoded (false) Default is false.
| contentTypeHeader | false | Boolean | Whether the data format should set the Content-Type header with the type from the data format if the data format is capable of doing so. For example application/xml for data formats marshalling to XML, or application/json for data formats marshalling to JSon etc.
|===
// dataformat options: END

== Message Headers (marshal)

[width="100%",cols="20%,20%,60%",options="header",]
|=======================================================================
|Name |Type |Description

|Message-Id |String |The marshal operation will set this parameter to the generated MIME
message id if the "headersInline" parameter is set to false.

|MIME-Version |String |The marshal operation will set this parameter to the applied MIME
version (1.0) if the "headersInline" parameter is set to false.

|Content-Type |String |The content of this header will be used as a content type for the
message body part. If no content type is set, "application/octet-stream"
is assumed. After the marshal operation the content type is set to
"multipart/related" or empty if the "headersInline" parameter is set to
true.

|Content-Encoding |String |If the incoming content type is "text/*" the content encoding will be
set to the encoding parameter of the Content-Type MIME header of the
body part. Furthermore the given charset is applied for text to binary
conversions.
|=======================================================================

== Message Headers (unmarshal)

[width="100%",cols="20%,20%,60%",options="header",]
|=======================================================================
|Name |Type |Description

|Content-Type |String |If this header is not set to "multipart/*" the unmarshal operation will
not do anything. In other cases the multipart will be parsed into a
camel message with attachments and the header is set to the Content-Type
header of the body part, except if this is application/octet-stream. In
the latter case the header is removed.

|Content-Encoding |String |If the content-type of the body part contains an encoding parameter this
header will be set to the value of this encoding parameter (converted
from MIME endoding descriptor to Java encoding descriptor)

|MIME-Version |String |The unmarshal operation will read this header and use it for parsing the
MIME multipart. The header is removed afterwards
|=======================================================================

== Examples

[source,java]
-----------------------------------
from(...).marshal().mimeMultipart()
-----------------------------------

With a message where no Content-Type header is set, will create a
Message with the following message Camel headers:

*Camel Message Headers*

-------------------------------------------------------------------------------
Content-Type=multipart/mixed; \n boundary="----=_Part_0_14180567.1447658227051"
Message-Id=<...>
MIME-Version=1.0
-------------------------------------------------------------------------------

-------------------------
The message body will be:
-------------------------

*Camel Message Body*

----------------------------------------------------------------
------=_Part_0_14180567.1447658227051
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
Qm9keSB0ZXh0
------=_Part_0_14180567.1447658227051
Content-Type: application/binary
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="Attachment File Name"
AAECAwQFBgc=
------=_Part_0_14180567.1447658227051--
----------------------------------------------------------------

A message with the header Content-Type set to "text/plain" sent to the
route

[source,java]
------------------------------------------------------------------------------------
from("...").marshal().mimeMultipart("related", true, true, "(included|x-.*)", true);
------------------------------------------------------------------------------------

will create a message without any specific MIME headers set as Camel
headers (the Content-Type header is removed from the Camel message) and
the following message body that includes also all headers of the
original message starting with "x-" and the header with name "included":

*Camel Message Body*

----------------------------------------------------------------
Message-ID: <...>
MIME-Version: 1.0
Content-Type: multipart/related; 
    boundary="----=_Part_0_1134128170.1447659361365"
x-bar: also there
included: must be included
x-foo: any value
 
------=_Part_0_1134128170.1447659361365
Content-Type: text/plain
Content-Transfer-Encoding: 8bit

Body text
------=_Part_0_1134128170.1447659361365
Content-Type: application/binary
Content-Transfer-Encoding: binary
Content-Disposition: attachment; filename="Attachment File Name"

[binary content]
------=_Part_0_1134128170.1447659361365
----------------------------------------------------------------

== Dependencies

To use MIME-Multipart in your Camel routes you need to add a dependency
on *camel-mail* which implements this data format.

If you use Maven you can just add the following to your pom.xml:

[source,xml]
-----------------------------------------------------------------------------------
<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-mail</artifactId>
  <version>x.x.x</version> <!-- use the same version as your Camel core version -->
</dependency>
-----------------------------------------------------------------------------------
