9.2 Creating a Simple MIME Part
This section describes the process for creating a simple MIME part that can also be transmitted as a standalone message.
A MIME part that contains simple data (rather than another MIME part) is the basic building block of the MIME format. Even the most complicated MIME message boils down to a collection of simple MIME parts. Since the MIME specification defines a MIME message as any MIME part that contains a MIME-Version header, the procedure in this section applies equally to creating a MIME part.
To create a simple MIME message:
1. Prepare the data, as described in Section 9.1.
2. Create headers for the message (Section 9.2.1).
3. Create an RWMimePart (Section 9.2.2).
4. Insert the headers into the message (Section 9.2.3).
5. Insert the message body into the message (Section 9.2.4).
6. Create a MIME string (Section 9.2.5).
9.2.1 Create Headers for the Part
Create headers describing the content of the message. Create any protocol-specific headers the part requires.
*Is the part body encoded, or 8-bit ASCII data, or binary data?
If so, create a Content-Transfer-Encoding header stating the datatype (for unencoded data) or the encoding (for encoded data). For example:
 
RWMimeContentTransferEncodingHeader
cTEHeader(RWMimeUtils::Base64);
*Is the part body a content type other than plain text?
If so, create a Content-Type header stating the type of data. For example, the code snippet below creates a header for a JPEG image:
 
RWMimeContentType imageJPG("image", "jpeg");
RWMimeContentTypeHeader contentTypeHeader(imageJPG);
*Will this part be a top-level message?
If so, create a default-constructed RWMimeVersionHeader.
 
RWMimeVersionHeader version;
MIME parts often require protocol-specific headers in addition to MIME headers. For example, email messages require a Date header and a From header. The RWMimeGenericHeader class represents a general-purpose header. If the part requires headers that aren’t part of the MIME format, use instances of RWMimeGenericHeader for those headers. For example, the line below creates a From header for an email message:
 
RWMimeGenericHeader from("From:", "Dev <developer@roguewave.com>");
9.2.2 Create an RWMimePart
Now that the content and the headers are created, construct an RWMimePart object, as shown below:
 
RWMimePart message;
9.2.3 Add the Headers to the Part
Add the headers to the part using the insertHeader() function, for example:
 
message.insertHeader(from);
The MIME specification requires that a MIME message contain a MIME-Version header. Be sure to insert the header into a part that will be used as a top-level message, as shown below:
 
message.insertHeader(version);
9.2.4 Add the String to the Part
Add the string to the MIME part using the setBody() function. This function sets the body of the MIME part to the provided string. For example:
 
RWCString messageBody;
 
// ... fill messageBody with data, convert to canonical form if
// necessary, encode if necessary.
 
RWMimePart message;
message.setBody(messageBody);
For a MIME part with simple content, the setBody() function does not process the provided string at all. The programmer is responsible for making sure that the headers of the part accurately describe the content in the string.
9.2.5 Create a MIME String
The MIME part is now complete and ready to use. To send the part over a network, convert the message into a MIME string using the asString() function, as shown:
 
RWMimePart message;
 
// ... fill message as described above
 
RWCString msgString;
 
try
{
msgString = message.asString();
}
catch (const RWMimeError& msg)
{
// Handle the error
}
The function doesn’t enforce a particular part style, or even guarantee that the part is correct MIME. The function does contain some safeguards that prevent a program from generating an unintelligible part. If the function can’t create a structurally correct MIME string, the function throws RWMimeError.