Quantcast
Channel: SCN : Blog List - Process Integration (PI) & SOA Middleware
Viewing all articles
Browse latest Browse all 676

Create email with body and attachments for binary payload with Java mapping

$
0
0

Some years ago I wrote a blog about emails with body and attachment with help of the MailPackage structure:

XI Mail Adapter: An approach for sending emails with attachment with help of Java mapping

 

In this blog I will present another solution which does not use MailPackage and I show also how to add binary payloads to an email.

 

Meanwhile there are many blogs available that show the use of the mail adapter and module configuration. However, using a Java mapping to create the whole MIME stream is the most flexible way to create a mail exactly in the way that it should look like.

 

The following code should show the basics of the MIME creation, feel free to use and enhance it to your needs:

 

Java Mapping to create MIME parts of an email

package sample;

 

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

 

import javax.xml.bind.DatatypeConverter;

 

import com.sap.aii.mapping.api.AbstractTransformation;

import com.sap.aii.mapping.api.StreamTransformationException;

import com.sap.aii.mapping.api.TransformationInput;

import com.sap.aii.mapping.api.TransformationOutput;

 

 

public class MyBinaryMessageAsAttachment extends AbstractTransformation {

 

  String attachmentName = "file.pdf";

  String boundary = "--AaZz";

  String mailContent = "This is a sample file";

  String CRLF = "\r\n";

 

  public void transform(TransformationInput arg0, TransformationOutput arg1)  throws StreamTransformationException {

 

    InputStream in = arg0.getInputPayload().getInputStream();

    OutputStream out = arg1.getOutputPayload().getOutputStream();

 

 

    try {

      // create the declaration of the MIME parts

      //First part

      String output = "--" + boundary + CRLF

        + "Content-Type: text/plain; charset=UTF-8" + CRLF

        + "Content-Disposition: inline" + CRLF + CRLF

        + mailContent // this should be some more useful text

        + CRLF + CRLF

 

      //Second part

        + "--" + boundary + CRLF

        + "Content-Transfer-Encoding: base64" + CRLF

        + "Content-Type: application/pdf; name=" + attachmentName + CRLF

        + "Content-Disposition: attachment; filename=" + attachmentName + CRLF + CRLF;

      out.write(output.getBytes());

 

      // convert InputStream to Byte array

      byte[] input = new byte[in.available()];

      in.read(input);

 

      // convert payload to base64

      output = DatatypeConverter.printBase64Binary(input);

 

      // split lines after 76 rows

      output = addLinefeeds(output);

      out.write(output.getBytes());

 

      // last boundary

      output = CRLF + CRLF +"--" + boundary + "--" + CRLF;

      out.write(output.getBytes());

    } catch (IOException e) {

      throw new StreamTransformationException(e.getMessage());

    }

  }

 

  public String addLinefeeds(String str) {

 

    StringBuffer result = new StringBuffer(str);

    int n = 76; // split by 76 characters (maximum of numbers in lines)

    int l = str.length();

    int i = n;

    while (l > n) {

      result.insert(i, CRLF);

      i = i + n + 2;

      l = l - n;

    }

    return result.toString();

  }

}

 

The Java Mapping will create two MIME parts. The first part is plain text, the second part is a binary, therefore we encode it to base64 and divide it into lines with 76 rows (which is the allowed maximum according to MIME protocol). The result will look like this:

 

Sample output of Java mapping

----AaZz

Content-Type: text/plain; charset=UTF-8

Content-Disposition: inline

 

 

This is a sample file

 

 

----AaZz

Content-Transfer-Encoding: base64

Content-Type: application/pdf; name=file.pdf

Content-Disposition: attachment; filename=file.pdf

 

 

PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPGNmZGk6Q29tcHJvYmFudGUg

cnRlPSIxNjA4Ni4xMyI+PC9jZmRpOlRyYXNsYWRvPjwvY2ZkaTpUcmFzbGFkb3M+PC9jZmRpOklt

cHVlc3Rvcz48Y2ZkaTpDb21wbGVtZW50bz48L2NmZGk6Q29tcGxlbWVudG8+PC9jZmRpOkNvbXBy

b2JhbnRlPg==

 

 

----AaZz--

 

This scenario requires special settings of the Mail adapter channel.

First of all, It is very important that the mail attribute Use Mail Package is not checked,Content Encoding is set to None and Keep Attachments is not checked.


Furthermore, we need to set a special Content-Type multipart/mixed; boundary="--AaZz"

The boundary declaration in the Content-Type must be identical with the boundary used in the Java mapping, otherwise the mail will just be the whole MIME stream as plain text.


We set the Content-Type with the MessageTransformBean in the Processing Sequence under tab Module like this:


 

ModuleTypeModule Key
AF_Modules/MessageTransformBeanLocal Enterprise BeanContentType
sap.com/com.sap.aii.adapter.mail.app/XIMailAdapterBeanLocal Enterprise Beanmail


 

Module KeyParameter NameParameter Value
ContentTypeTransform.ContentTypemultipart/mixed; boundary="--AaZz"

 

If you want to know, how the MIME parameters Content-Type and Content-Transfer-Encoding work, and what other parameters can be used, then you can look in RFC 1341 about MIME (Multipurpose Internet Mail Extensions): http://www.w3.org/Protocols/rfc1341/0_TableOfContents.html


Viewing all articles
Browse latest Browse all 676

Trending Articles