Sending a MIME message

You can send an email that is constructed from a MIME message.

About this task

You can pass a MIME message to the EmailOutput node, which uses the MIME parser to write the MIME message to a bit stream. This message is then sent to the list of recipients in the EmailOutputHeader. Local environment overrides are not considered when a MIME message is passed. You do not need to configure the EmailOutput node in the following examples.

The following examples show how to set up the recipient, sender, subject, SMTP server, and message body information in ESQL (with a Compute node) and Java™ (with a JavaCompute node).

Using a Compute node

About this task

The diagram shows a message flow that uses an MQInput node, Compute node, and EmailOutput node to send a MIME message.
CREATE FUNCTION Main() RETURNS BOOLEAN
BEGIN
		
	  CALL CopyMessageHeaders();
		
	  -- Add recipient information to the EmailOutputHeader
	  SET OutputRoot.EmailOutputHeader.To = '<recipient email address>';
	  SET OutputRoot.EmailOutputHeader.Cc = '<recipient email address>';
	  SET OutputRoot.EmailOutputHeader.Bcc = '<recipient email address>';
	  
	  -- Add sender information to EmailOutputHeader
	  SET OutputRoot.EmailOutputHeader.From = '<sender email address>';
	  SET OutputRoot.EmailOutputHeader."Reply-To" = '<reply email address>';

	  -- Add subject to EmailOutputHeader
	  SET OutputRoot.EmailOutputHeader.Subject = 'Dynamic MIME message in ESQL.';

	  -- Add SMTP server information to the LocalEnvironment
	  SET OutputLocalEnvironment.Destination.Email.SMTPServer ='<smtp.server:port>';

	  -- Create a new MIME message body, which will be sent as the main text of the email,
	  -- including an attachment.
	  CREATE FIELD OutputRoot.MIME TYPE Name;
	  DECLARE M REFERENCE TO OutputRoot.MIME;

	  -- Create the Content-Type child of MIME explicitly to ensure the correct order. If we set
	  -- the ContentType property instead, the field could appear as the last child of MIME.
	  CREATE FIELD M."Content-Type" TYPE NameValue VALUE 'multipart/related; boundary=myBoundary';
	  CREATE FIELD M."Content-ID"   TYPE NameValue VALUE 'new MIME document';

	  CREATE LASTCHILD OF M TYPE Name NAME 'Parts';
	  CREATE LASTCHILD OF M.Parts TYPE Name NAME 'Part';
	  DECLARE P1 REFERENCE TO M.Parts.Part;

	  -- First part:
	  --   Create the body of the email.
	  --   The body of the email has the text 'This is the main body of the email.'.
	  CREATE FIELD P1."Content-Type" TYPE NameValue VALUE 'text/plain; charset=us-ascii';
	  CREATE FIELD P1."Content-Transfer-Encoding" TYPE NameValue VALUE '8bit';
	  CREATE LASTCHILD OF P1 TYPE Name NAME 'Data';
	  CREATE LASTCHILD OF P1.Data DOMAIN('BLOB') PARSE(CAST('This is the main body of the email.' 
AS BLOB CCSID 1208));
	  
	  CREATE LASTCHILD OF M.Parts TYPE Name NAME 'Part';
	  DECLARE P2 REFERENCE TO M.Parts.Part[2];

	  -- Second part:
	  --   Create the attachment of an email.
	  --   The attachment is called 'attachment.txt' and contains the text 'This is an attachment.'.
	  CREATE FIELD P2."Content-Type" TYPE NameValue VALUE 'text/plain; charset=us-ascii; name=attachment.txt';
	  CREATE FIELD P2."Content-Transfer-Encoding" TYPE NameValue VALUE '8bit';
	  CREATE LASTCHILD OF P2 TYPE Name NAME 'Data';
	  CREATE LASTCHILD OF P2.Data DOMAIN('BLOB') PARSE(CAST('This is an attachment.' AS BLOB CCSID 1208));

	  RETURN TRUE;
END;

Using a JavaCompute node

About this task

The diagram shows a message flow that uses an MQInput node, JavaCompute node, and EmailOutput node to send a MIME message.
public void evaluate(MbMessageAssembly assembly) throws MbException {
	  MbOutputTerminal out = getOutputTerminal("out");
	  MbOutputTerminal fail = getOutputTerminal("fail");

	  // Create a new assembly to propagate out of this node, as we want to
	  // update it
	  MbMessage outMessage = new MbMessage();
	  copyMessageHeaders(assembly.getMessage(), outMessage);
	  MbMessage outLocalEnv = new MbMessage(assembly.getLocalEnvironment());
	  MbMessage outExceptionList = new MbMessage(assembly.getExceptionList());
	  MbMessageAssembly outAssembly = new MbMessageAssembly(assembly, outLocalEnv, outExceptionList, outMessage);
	  MbElement localEnv = outAssembly.getLocalEnvironment().getRootElement();

	  // Create the EmailOutputHeader parser. This is where we add recipient,
	  // sender and subject information.
	  MbElement root = outMessage.getRootElement();
	  MbElement SMTPOutput = root.createElementAsLastChild("EmailOutputHeader");

	  // Add recipient information to EmailOutputHeader
	  SMTPOutput.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "To", "<recipient email address>");
	  SMTPOutput.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Cc", "<recipient email address>");
	  SMTPOutput.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Bcc", "<recipient email address>");

	  // Add sender information to EmailOutputHeader
	  SMTPOutput.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "From", "<sender email address>");
	  SMTPOutput.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Reply-To", "<reply email address>");

	  // Add subject information to EmailOutputHeader
	  SMTPOutput.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Subject", "Dynamic MIME message in Java.");

	  // Create Destination.Email. This is where we add SMTP server information.
	  MbElement Destination = localEnv.createElementAsLastChild(MbElement.TYPE_NAME, "Destination", null);
	  MbElement destinationEmail = Destination.createElementAsLastChild(MbElement.TYPE_NAME, "Email", null);
	  destinationEmail.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "SMTPServer", "<smtp.server:port>");

	  // Set last child of root (message body) as MIME.
	  MbElement MIME = root.createElementAsLastChild("MIME");

	  // Create the Content-Type child of MIME explicitly to ensure the correct order.
	  MIME.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Content-Type", "multipart/related; 
boundary=myBoundary");
	  MIME.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Content-ID", "new MIME document");
	  MbElement parts = MIME.createElementAsLastChild(MbElement.TYPE_NAME, "Parts", null);
	  MbElement part, data, blob;
	  String text;

	  // First part:
	  //   Create the body of the email.
	  //   The body of the email has the text 'This is the main body of the email.'.
	  part = parts.createElementAsLastChild(MbElement.TYPE_NAME, "Part", null);
	  part.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Content-Type", "text/plain; charset=us-ascii");
	  part.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Content-Transfer-Encoding", "8bit");
	  data = part.createElementAsLastChild(MbElement.TYPE_NAME, "Data", null);
	  blob = data.createElementAsLastChild("BLOB");
	  text = "This is the main body of the email.";
	  try {
		blob.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "BLOB", text.getBytes("UTF8"));
	  } catch (UnsupportedEncodingException e) {
		fail.propagate(outAssembly);
	  }

	  // Second part:
	  //   Create the attachment of an email.
	  //   The attachment is called 'attachment.txt' and contains the text 'This is an attachment.'.
	  part = parts.createElementAsLastChild(MbElement.TYPE_NAME, "Part", null);
	  part.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Content-Type", "text/plain; charset=us-ascii; 
name=attachment.txt");
	  part.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Content-Transfer-Encoding", "8bit");
	  data = part.createElementAsLastChild(MbElement.TYPE_NAME, "Data", null);
	  blob = data.createElementAsLastChild("BLOB");
	  text = "This is an attachment.";
	  try {
		blob.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "BLOB", text.getBytes("UTF8"));
	  } catch (UnsupportedEncodingException e) {
		fail.propagate(outAssembly);
	  }
	
	  outMessage.finalizeMessage(MbMessage.FINALIZE_VALIDATE);
	  out.propagate(outAssembly);
}