Program: Creating a Simple MIME Message
Example 28 uses the MIME package to generate a simple MIME email message. The source code for this program is located in the file BasicMimeMessageComposition.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 28 – Creating a simple MIME message
#include <rw/mime/RWMimePart.h>
#include <rw/mime/RWMimeTextType.h>
#include <rw/mime/RWMimeUtils.h>
#include <rw/mime/RWMimeGenericHeader.h>
#include <rw/mime/RWMimeVersionHeader.h>
 
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
 
int main(void)
{
// Generate the headers required for the message.
// Use a generic header to create headers that don't have a
// derived header type.
RWMimeGenericHeader toHeader("To",
"<developer@yourcompany.com>"); //1
RWMimeGenericHeader fromHeader("From",
"<developer@roguewave.com>");
RWMimeGenericHeader subjectHeader("Subject",
"Sample MIME Message from Rogue Wave");
// We also want to send a "Content-Type" header to identify
// the content of the message. Since this is a plain text
// message, we'll use a RWMimeTextType object to generate
// the header.
RWMimeTextType textType("plain"); //2
// Define the body of the message.
RWCString messageBody(
"This is a simple MIME Message generated with the\n"
"Internet Protocols Module of SourcePro Net.\n"); //3
// It's a good idea to convert the message body over to a
// canonical form before sending it, to insure that the
// meaning of the message is not modified in transit.
messageBody = RWMimeUtils::replaceLineDelimiter(messageBody); //4
// Create the basic message object. This will hold all of
// the message parts, and format them according to the MIME
// specification.
RWMimePart message(textType); //5
 
// Add each of the headers to the message.
message.insertHeader(RWMimeVersionHeader()); //6
message.insertHeader(toHeader); //7
message.insertHeader(fromHeader);
message.insertHeader(subjectHeader);
 
// Add the body to the message.
message.setBody(messageBody); //8
 
// The message is ready to go. Normally you would send this
// through a protocol (SMTP, HTTP), but for this example we
// will just print it to stdout.
 
cout << message.asString() << endl; //9
return 0;
}
 
//1 First, the application generates headers for the email message. This line creates the “To:” header required for an Internet email message. Since RWMimeGenericHeader meets the requirements for an RFC 2822 header, we use instances of RWMimeGenericHeader for the email headers in the message. The next two lines of code create the “From:” and “Subject:” headers needed for an email message.
//2 Creates an RWMimeTextType for the value "text/plain". This is the content type of an email message.
//3 Constructs an RWCString containing the body of the message. Notice that the line breaks in the message text are newline (LF) characters.
//4 Converts the message to canonical form. Since the body is already in 7-bit ASCII text with lines that are less than 78 characters, there’s no need to encode the message.
//5 Creates an RWMimePart. The constructor adds a Content-Type header with the value textType.
//6 Creates an MIME-Version header and adds the header to the message.
//7 Inserts the “To:” header into the message. The next few lines add the other email headers to the message.
//8 Adds the message body to the message.
//9 Prints the message to standard output. A production application would typically enclose the asString() function in a try block, since the function throws an RWMimeError if it can’t create a MIME string with the correct syntax. Since this example uses a constant string, we’ve left out the try block for clarity.
The program produces the following output:
 
Content-Type: text/plain
MIME-Version: 1.0
To: <developer@yourcompany.com>
From: <developer@roguewave.com>
Subject: Sample MIME Message from Rogue Wave
 
This is a simple MIME Message generated with the
Internet Protocols Module of SourcePro Net.