Sun Java System Messaging Server 6 2005Q4 MTA Developer's Reference

A Simple Example of Enqueuing a Message

The program shown in Example 3–1 demonstrates how to enqueue a simple “Hello World” message. The originator address associated with the message is that of the MTA postmaster. The recipient address can be specified on the invocation command line.

After the Messaging Server product is installed, this program can be found in the following location:

msg_server_base/examples/mtasdk/

Note that certain lines of code have numbered comments immediately preceding them of the format:

/* This generates output line N */

where N corresponds to the numbers found next to certain output lines in the sample output in Enqueuing a Message Example Output.

Refer to Running Your Test Programs for information on how to run the sample program.


Example 3–1 Enqueuing a Message


/* hello_world.c -- A simple "Hello World!" enqueue example */
#include <stdio.h\>
#include <stdlib.h\>
#include "mtasdk.h"

mta_nq_t *ctx = NULL;
static void quit(void);
#define CHECK(x) if(x) quit();

void main(int argc, const char *argv[])
{
     char buf[100];

     /* Initialize the SDK */
     CHECK(mtaInit(0));

     /* Start a new message; From: postmaster*/
     /* This generates output line 1 */
     CHECK(mtaEnqueueStart(&ctx, mtaPostmasterAddress(NULL, NULL,
                           0), 0, 0));

     /* Enqueue the message to argv[1] or root */
     /* This generates output line 2 */
     CHECK(mtaEnqueueTo(ctx, (argv[1] ? argv[1] : "root"), 0, 0));

     /* Date: header line */
     /* This generates output line 3 */
     CHECK(mtaEnqueueWriteLine(ctx, "Date: ", 0, mtaDateTime(buf,
                               NULL, sizeof(buf), 0), 0, NULL))

     /* Subject: header line */
     /* This generates output line 4 */

     CHECK(mtaEnqueueWriteLine(ctx, "Subject: " __FILE__, 0,
                               NULL));

     /* Blank line ending the header, starting the message body */
     /* This generates output line 5 */
     CHECK(mtaEnqueueWriteLine(ctx, "", 0, NULL));

     /* Text of the message body (2 lines) */
     /* This generates output line 6 */
     CHECK(mtaEnqueueWriteLine(ctx, "Hello", 0, NULL));
     /* This generates output line 7 */
     CHECK(mtaEnqueueWriteLine(ctx, " World!", 0, NULL));

     /* Enqueue the message */
     CHECK(mtaEnqueueFinish(ctx, 0));

     /* All done */
     mtaDone();
}

void quit(void)
{
     fprintf(stderr, "The MTA SDK returned the error code %d\n
             %s", mta_errno, mtaStrError(mta_errno, 0));
     if (ctx)
         mtaEnqueueFinish(ctx, MTA_ABORT, 0);
     exit(1);
}

Enqueuing a Message Example Output

The example that follows shows the output generated by the enqueuing example. Comment numbers correspond to the numbered comments in Example 3–1.

Comment Number 

Output Lines 

 

Received:from siroe.com by siroe.com (SunONE Messaging Server 6.0)id

<01GP37SOPRW0A9KZFV@siroe.com\>; Fri, 21 Mar 2003 09:07:32 -0800(PST)

Date: Fri, 21 Mar 2003 09:07:41 -0800 (PST)

From: postmaster@siroe.com

To: root@siroe.com

Subject: enqueuing_example.c Message-id: <01GP37SOPRW2A9KZFV@siroe.com\> Content-type: TEXT/PLAIN; CHARSET=US-ASCII Content-transfer-encoding: 7BIT

Hello

World!