Program: Processing a Simple MIME Message
Example 30 uses the MIME package to process a simple MIME message and print the components of the message to standard output. The message processed is essentially identical to the message generated in Example 28, Creating a simple MIME message. We’ve taken shortcuts to keep the example simple. A production application would normally check to make sure that the message received is actually a simple MIME message before processing the message (see Process the Body). Source for this program is in BasicMimeMessageDecomposition.cpp in the examples\mime directory.
NOTE: Sample programs are located in the examples directory created for your installation. For more information, see Installing and Building Your SourcePro Products and Building Your Applications.
Example 30 – Processing a simple MIME message
 
#include <rw/mime/RWMimePart.h>
 
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
 
// This is roughly equivalent to the message generated in
// the BasicMimeMessageComposition example.
const RWCString messageStr(
"MIME-Version: 1.0\r\n"
"To: <developer@yourcompany.com>\r\n"
"From: <developer@roguewave.com>\r\n"
"Subject: Sample MIME Message from Rogue Wave\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"This is a simple MIME Message generated with the\r\n"
"Internet Protocols Module of SourcePro Net.\r\n"); //1
 
int main(void)
{
// Create a RWMimePart object.
RWMimePart message; //2
// extract the message from the string.
message.fromString(messageStr); //3
 
cout << "MIME Message Extraction" << endl << endl;
// Show header information for the message.
cout << "...Headers" << endl << endl;
 
for (size_t pos = 0; pos < message.getHeaderCount(); ++pos) //4
{
RWMimeHeader header = message.getHeader(pos); //5
cout << header.asString() << endl; //6
}
// Display the body of the message. Normally, you would
// convert line delimiters into local form for your OS
// before displaying a text body. But for this example,
// we'll just print it out in canonical form.
cout << endl << "...Body" << endl << endl << message.getBody() << endl; //7
 
return 0;
}
 
 
//1 Defines the message as a constant string to keep the example short. A production application would obtain the message from outside the program.
//2 Creates an empty RWMimePart.
//3 Populates the part with the contents of messageStr. In a production application, the call to fromString() would be enclosed in a try block. We’ve left out the try block since the example uses a constant string.
//4 The for loop on this line increments pos to index each header in the message. To find a specific header, you could also use the findHeader() function of RWMimePart.
//5 Retrieves the header at position pos.
//6 Prints the header label and the header value to standard output.
//7 Prints the message body to standard output. This command prints the message body exactly as the body appears in the MIME message. A production application would typically decode the body and convert the body from canonical text before displaying the body or processing the body further.
This program generates the following output:
 
MIME Message Extraction
 
...Headers
 
MIME-Version: 1.0
To: <developer@yourcompany.com>
From: <developer@roguewave.com>
Subject: Sample MIME Message from Rogue Wave
Content-Type: text/plain
 
...Body
 
This is a simple MIME Message generated with the
Internet Protocols Module of SourcePro Net.