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

Examples of Using mtaSend()

Several example programs, written in C, are provided in this section:

The example routines shown in this section may be found in the examples/mta/sdk directory.

Sending a Simple Message

The program shown in Example 7–1 demonstrates how to send a simple message to the root account. The source code itself is used as the input source for the body of the message to be sent. The From: address associated with the message is that of the process running the program. Comments in the program example explain the sample output line they generate.


Example 7–1 Send a Simple Message


/* send_simple.c Send a simple message */
#include <string.h\>
#include "mtasdk.h"

/* Push an entry onto the item list */
#define ITEM(item,adr) item_list[index].item_code = item;\
  item_list[index].item_address = adr;\
  item_list[index].item_length  = adr ? strlen(adr) : 0; \
  item_list[index].item_status  = 0;\
  item_list[index++].item_smessage = NULL

main ()
{
  mta_item_list_t item_list[4];
  int index = 0;

  ITEM(MTA_TO, "root"); /* Becomes the To: line in the output */
  ITEM(MTA_SUBJECT, "send_simple.c");
  ITEM(MTA_MSG_FILE, __FILE__);/* Becomes the Subject: line */
  ITEM(MTA_END_LIST, 0);
  exit(mtaSend(item_list));
}

Output for Example 1 Sending a Simple Message


Date: 04 Oct 1992 22:24:07 -0700 (PDT)
From: jdoe@sesta.com
Subject: send_simple.c
To: root@sesta.com
Message-id: <01GPKF10JIB89LV1WX@sesta.com\>
MIME-version: 1.0
Content-type: TEXT/PLAIN; CHARSET=US-ASCII
Content-transfer-encoding: 7BIT

/* send_simple.c -- Send a simple message */
#include <string.h\>
#include "mtasdk.h"

...

Example 2 Specifying an Initial Message Header

The program shown in Example 7–2 illustrates the use of the MTA_HDRMSG_FILE and MTA_HDR_ADRS item codes to enqueue a message that has already been composed, including the headers, and stored in a file. The input file is given in the Input File for Example 2 Specifying an Initial Message Header. The resulting message is shown in Output for Example 2 Specifying an Initial Message Header.

When the entire message, header and body, is contained in a single file, use the MTA_HDRMSG_FILE item code in place of the MTA_HDR_FILE and MTA_MSG_FILE item codes.


Example 7–2 Specify an Initial Message Header


/* send_header.c -- Send a message with initial header */
#include <string.h\>
#include "mtasdk.h"

/* Push an entry onto the item list */
#define ITEM(item,adr) item_list[index].item_code = item;\
  item_list[index].item_address = adr;\
  item_list[index].item_length  = adr ? strlen(adr) : 0;\
  item_list[index].item_status  = 0;\
  item_list[index++].item_smessage = NULL

main ()
{
  MTA_item_list_t item_list[3];
  int index = 0;

  ITEM(MTA_HDR_ADRS, 0);
  ITEM(MTA_HDRMSG_FILE, "send_header.txt");
  ITEM(MTA_END_LIST, 0);
  exit(mtaSend(item_list));
}

            

Input File for Example 2 Specifying an Initial Message Header


Subject: MTA SDK callable Send example
To: root@sesta.com
MIME-version: 1.0
Content-type: TEXT/PLAIN; CHARSET=US-ASCII
Content-transfer-encoding: 7BIT
Comments: Ignore this message -- it’s just a test

This is a test of the emergency broadcasting system!

1234567890123456789012345678901234567890123456789012345678901234
5678901234567890

0000000001111111111222222222233333333334444444444555555555566666
6666677777777778

Output for Example 2 Specifying an Initial Message Header


Date: 04 Jan 2003 22:42:25 -0800 (PST)
From: system@sesta.com
Subject: MTA SDK callable Send example
To: system@sesta.com
Message-id: <01GPKFNPUQF89LV1WX@sesta.com\>
MIME-version: 1.0
Content-type: TEXT/PLAIN; CHARSET=US-ASCII
Content-transfer-encoding: 7BIT
Comments: Ignore this message -- it’s just a test

This is a test of the emergency broadcasting system!

1234567890123456789012345678901234567890123456789012345678901234
5678901234567890

0000000001111111111222222222233333333334444444444555555555566666
6666677777777778

Example 3 Sending a Message to Multiple Recipients

The program given in Example 7–3 demonstrates the following points:

The message is sent to one To: address, a Cc: address, and a Bcc: address. After mtaSend() is called, any status message associated with each address is displayed.

The log output produced by running the program is shown in Output for Example 3 Sending a Message to Multiple Recipients.

The following items of note are identified in the comments in the program:


Example 7–3 Sending a Message to Multiple Recipients


/* send_multi.c -- Send a message to multiple recipients */
#include <stdio.h\>
#include <string.h\>
#include "mtasdk.h"

#define ITEM(item,adr) item_list[index].item_code = item;\
  item_list[index].item_address = adr;\
  item_list[index].item_length  = adr ? strlen(adr) : 0;\
  item_list[index].item_status  = 0;\
  item_list[index++].item_smessage = NULL

main ()
{
  int index = 0, istat, i;
  mta_item_list_t item_list[7];

  /* Specify the Subject: header line and message input source */
  ITEM(MTA_SUBJECT, "send_multi.c");
  ITEM(MTA_MSG_FILE, __FILE__);

  /* Return per address status/error messages */
  ITEM(MTA_ADR_STATUS, 0); /* Instructs mtaSend() to return a */
                           /* status message for each envelope */
                           /* recipient address                */

  /* Specify regular Bcc:, To:, and Cc: addresses */
  ITEM(MTA_BCC, "root");
  ITEM(MTA_TO, "abuse@sample.com");
  ITEM(MTA_CC, "postmaster@sample.com");

  /* Now terminate the item list */
  ITEM(MTA_END_LIST, 0);

  /* And send the message */
  istat = mtaSend(item_list);/* Sends the message. */

  /* Display the address status messages provided that no */
  /* error other than MTA_HOST has occurred               */



  for (i = 0; i < index; i++) /* Display any returned status */
                              /* messages                    */
       if (item_list[i].item_smessage)
           printf ("%s: %s - %s\n",
                   (const char *)item_list[i].item_address,
                   item_list[i].item_status ? "Failed" :
                                              "Succeeded",
                   item_list[i].item_smessage);

  /* Dispose of status messages */
  mtaSendDispose(item_list);
  exit(istat);
}

Output for Example 3 Sending a Message to Multiple Recipients


Succeeded: root@sample.com
Succeeded: abuse@sample.com
Succeeded: postmaster@sample.com

Example 4 Using an Input Procedure to Generate the Message Body

The program shown in Example 7–4 uses an input procedure as the source for the body of a message to be sent. In the program, the input procedure msg_proc will read input until the runtime library routine fgets() signals an EOF condition, for example, a control-D has been input. The address of the procedure msg_proc is passed to mtaSend() using a MTA_MSG_PROC item code. The mtaSend() routine repeatedly calls the msg_proc procedure, until a negative value is returned by the procedure.


Example 7–4 Using an Input Procedure to Generate the Message Body


/* send_input.c -- Demonstrate the use of MTA_MSG_PROC */
#include <stdio.h\>
#include <stdlib.h\>
#include <string.h\>
#include "mtasdk.h"
#ifdef _WIN32
typedef long ssize_t;
#endif

/* Push an entry onto the item list */
#define ITEM(item,adr) item_list[index].item_code = item;\
item_list[index].item_address = adr;\
item_list[index].item_length  = 0;\
item_list[index].item_status  = 0;\
item_list[index++].item_smessage = NULL

ssize_t msg_proc(const char **bufadr)
{
  static char buf[1024];

  if (!bufadr)
      return(-2); /* Call error; abort */

  printf("input: ");
  if (fgets(buf, sizeof(buf), stdin))
  {
    *bufadr = buf;
    buflen = strlen(buf);
    if (buf[buflen-1] == ’\n’)
      buflen -= 1;
    return(buflen);
  }
  else
    return(-1);  /* EOF */
}

main ()
{
  int istat, index = 0;
  mta_item_list_t item_list[4];

  STRITEM(MTA_SUBJECT, "send_input.c");
  STRITEM(MTA_TO, "root");
  ITEM(MTA_MSG_PROC, msg_proc);
  ITEM(MTA_END_LIST, 0);
  exit(mtaSend(item_list));
}