SAP Process Integration

File to Mail Attachment Scenario


This blog’s main objective is to consolidate different approaches to address below cases using PI/PO scenarios:

  • How to send a mail having a FlatFile as a mail-attachment
  • How to encrypt/sign the attachment on runtime before sending it to the mail
  • How to set mail-attachment name as of original file name without using any CustomAdpaterModule
  • How to set static mail-attachment name

Following business requirement was the motive of this exercise:

  • Read the file from SAP-R3 directory(NFS) or FTP or SFTP
  • Encrypt it using external-party’s PGP-Pub key and sign it using own passphrase
  • and send the encrypted/signed content as an attachment to a mail-id. The attachment should have original file name.

Approach-01: Retain mail-attachment name as of original file name

  • Use “Sender FILE CC (Communication Channel)” configuration to read the file from directory and applying Module Bean ‘PGPEncryption’ for Encryption/Signing of the file content
  • Use “Receiver MAIL CC” configuration to send the mail. Here “Mail Package” is been used.
  • And Use Java-Map in ESR (Enterprise Service Repository) to prepare “Mail Package XML”

Below is the Java-Map program to prepare “Mail Package XML”:

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;


public class FileToMailAttachment implements StreamTransformation{

	private Map param;
	public void setParameter(Map map) {
		param = map;
		if (param == null) {
			param = new HashMap();
	private static AbstractTrace trace = null;	
	public void execute(InputStream in, OutputStream out) throws StreamTransformationException {
		try {
			 * Required MAP_jar files: aii_map_api.jar
			 * Purpose of this JavaMap:
			 * 	Once File-Sender-Adapter reads file, sending this file as an attachment to Receiver Mail Adapter using MailPackage

			 * Functionality of This JavaMap:
			 * 1. Get FileName from ASMA DynamicConfiguration Variable
			 * 2. To just copy InputStream(FileContent read by FileAdapter) to String
			 * 3. Prepare "MailPackage_XML" having dynamic attachment name
			 * 4. Send MailPackage_XML to OutputStream
			trace = (AbstractTrace)param.get(StreamTransformationConstants.MAPPING_TRACE);
			trace.addInfo("Start of JavaMap class FileToMailAttachment()");
			//1. Get FileName from ASMA DynamicConfiguration Variable
			String strFileName = "";
		    DynamicConfiguration conf = (DynamicConfiguration)param.get("DynamicConfiguration");
			DynamicConfigurationKey key = DynamicConfigurationKey.create("","FileName");
			strFileName = conf.get(key);	//to get the value from the 'key'
			trace.addInfo("FileName: "+ strFileName );	 	
			//2. To just copy InputStream(FileContent read by FileAdapter) to String
			byte[] bytes = new byte[in.available()];;
			String fileContent= new String(bytes, "UTF-8");
			//3. Prepare "MailPackage_XML" having dynamic attachment name
			String subjectMail = "FileToMail_EncSigned_TEST";
			String fromMailId = "";
			String toMailId = "";			
			String mailPackage = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+ "\r\n" +
			"<ns:Mail xmlns:ns=\"\"> "+ 
				"<Subject>"+ subjectMail +"</Subject>" +
				"<From>"+ fromMailId +"</From>"+
				"<To>"+ toMailId +"</To>"+
				"<Reply_To />"+
				"<Content_Type>multipart/mixed;boundary = --AaZz</Content_Type>"+
					"\r\nContent-Type: text/plain; charset=UTF-8" + "\r\n" +
					"Content-Disposition: inline" + "\r\n" + "\r\n" +
					"The attachment \""+ strFileName  + "\" is a encrypted/signed document." + "\r\n" +
					"----AaZz" + "\r\n" +
					"Content-Type: application/xml; name=" + strFileName  + "\r\n" +
					"Content-Disposition: attachment; filename=" + strFileName + "\r\n" + 
					"\r\n" + fileContent +  "\r\n"+			  		
			//4. Send MailPackage_XML to OutputStream
			trace.addInfo("End of JavaMap class FileToMailAttachment()");			
		}catch(Exception e){
			trace.addInfo("Exception in JavaMap FileToMailAttachment(): " + e.getMessage());
	public static void main(String[] args) {
		try {
			String filePath = "C:\\Users\\xyz\\Desktop\\TEST\\";				
			String Fnm = "Input_Sample1.csv";
			String inputFile = filePath + Fnm;	
			String outputFile = filePath + "Output_" + Fnm ;

			FileToMailAttachment myClass = new FileToMailAttachment();
			FileInputStream in = new FileInputStream(inputFile);
			FileOutputStream out = new FileOutputStream(outputFile);
			myClass.execute(in, out);
		}catch(Exception e){

Following screen displays “Sender FILE CC” configuration:

And on this “Sender FILE CC”, apply Module Bean “PGPEncryption” as follows, where

applySignature =true   This is to Sign the FileContent using own passphrase
keyRootPath = <>       The path where partner’s PGP_Public_Key and own PrivatePublic_Key is present
ownPrivateKey = <>     Own PrivatePublic_Key file name
partnerPublicKey =<>   Partner’s PGP_Public_Key file name
pwdOwnPrivateKey=<>    Own passphrase/password

And if we want to send the raw file content (without Encryption/Signing), then Only use default Module Bean ‘CallSapAdapter’

Following screen displays “Receiver MAIL CC” configuration:

Use of IMAP4 is been shown here, We can used SMTP too.

In TAB ‘General’ TAB, provide Mail Server URL and check checkBox ‘Use Mail Package’

In TAB ‘Advanced’ TAB, check CheckBox ‘Use Adapter-Specific Message Attributes’

In TAB ‘Module’, default module selection

Output/TEST reference: Following is the test screen, where, using above PI/PO interface configuration, the file “Sample.txt” was been read from directory, its been been encrypted/signed and using JavaMap, respective ‘Mail-Package’ was been prepared and this was sent to Receiver-Mail-CC which result into below Mail-Screen:

Approach-02: Set Static mail-attachment name

  • We can design a simple pass through PI/PO scenario (without ESR) to achieve above requirement’s objective, but when we want to set STATIC attachment name
  • Use “Sender FILE CC” to read the files from SAP-R3 directory.
  • Use “Receiver MAIL CC” to encrypt/sign the file content and send this Encrypted/Signed content as an attachment to the mail-id.
  • On “Receiver MAIL CC”, use Module Bean “MessageTransformBean” to rename attachment to a fix static name.
  • On “Receiver MAIL CC”, use Module Bean “PGPEncryption” to perfom Encryption/Signing of the payload/FileContent
  • Note: Here, if we do not use “PGPEncryption” Module on “Sender FILE CC” because it sends payload to “Receiver MAIL CC” as a HTML content having BODY with Encrypted/Signed string

Following screen displays “Receiver MAIL CC” configuration:

And on this “Receiver MAIL CC“, apply Module Bean “MessageTransformBean” as follows:

And on this “Receiver MAIL CC“, apply Module Bean “PGPEncryption” as follows in given sequence if we want to encrypt/sign the file Content:

Output/TEST reference:

Leave a Reply

Your email address will not be published. Required fields are marked *