9.3 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.
Sample programs are located in the examples directory created for your installation. For more information, see Installing and Building Your SourcePro C++ 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;
}
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.