Sending an Email Attachment with Dynamic File Name and Custom Email Body

Hi All,

I have designed a requirement to send an Email attachment with dynamic file name and custom email body for a pass through scenario using Java Mapping.

Introduction:

The requirement is to read files from the SFTP server and send each file in an email as an attachment with dynamic File Name and custom Emaill body

Even the subject name should be dynamic i.e it should have the same name like the attachment name.

SAP PI Scenario details:

File to Mail interface is created to achieve the above requirement.The interface will read files using SFTP Sender Adapter and will send each file as an attachment using Receiver Mail Adapter with custom email body and attachment name.

Pre-requisites:

SAP-PI should have SMTP mail server accessibility.
SAP-PI should have access to SFTP server’s directory.

Below is Java Code:

package Mail;
import java.io.InputStream;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.Attachment;
import com.sap.aii.mapping.api.DynamicConfiguration;
import com.sap.aii.mapping.api.DynamicConfigurationKey;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;

public class AttachmentName extends AbstractTransformation {
 /*
  * This method takes the input payload from the Transformation Input Object and sends the updated output payload in the Transformation Output object.
  */
 @Override
 public void transform(TransformationInput in , TransformationOutput out) throws StreamTransformationException {
  try {

   String myparameter = in.getInputParameters().getString("SubjectName"); //gets the subject name from Parameterised mapping														
   DynamicConfiguration dc = in.getDynamicConfiguration(); //getting the instance of Dynamic Configuration to access the adapter specific attributes																
   DynamicConfigurationKey dckey = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File", "FileName"); //creating a Dynamic Configuration key to get the Input File FileName
   DynamicConfigurationKey subkey = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/Mail", "THeaderSUBJECT"); //creating a Dynamic Configuration key to set the Subject in receiver Mail Adapter
   String filename = dc.get(dckey); //gets the Input File name 
   String fname = filename + ".TXT"; //prepare the FileName with Extension .TXT
   String subjectname = myparameter + filename; //prepare the subject name by concatenating with Input FileName
   dc.put(subkey, subjectname); //updating the Mail Subject																										
   InputStream is = in.getInputPayload().getInputStream(); //reads the Input Main Payload
   byte buffer[] = new byte[is.available()]; //create buffer as per InputStream size
   is.read(buffer); //read data into buffer
   Attachment attch = out.getOutputAttachments().create(fname, "text/plain", buffer); //create the attachment using the Input message Payload and also setting the FileName for the attachment
   out.getOutputAttachments().setAttachment(attch); //setting the attachment
   // creating a HTML Mail body for the target Mail Adapter
   out.getOutputPayload().getOutputStream().write("<html><head></head><body><table><tr><td ><b>Alert Notification for file confirmation</b></td></tr><tr><td> Hi,<br><br>This is a xyz Bank EFT Pay Confirmation Acknowledgement</td></tr><br><tr><td><br><i> Note : This is auto-generated mail. Please do not reply</i></td></tr><tr><td></td></tr><tr><td><br>Regards</td></tr><tr><td> SAP PO Team</td></tr></table></body></html>".getBytes());
  } catch (Exception e) {
   throw new StreamTransformationException("Error in java mapping:" + e.getMessage()); //any exception occured in mapping execution will be captured here
  }
 }
}

 

Mail Adapter screenshots:

Defining Parameter for Subject Name:

Received Email:

Conclusion:

For a pass through scenario if you want to change any parameters like File Name , Subject Name for a receiver Mail or File  you need to use Java Mapping. You can maintain the HTML body in the parameters as shown for Subject Name in the above screenshots and call them in our Java mapping which helps you not to transport if you do any changes in the body.

Hope this blog helps you in understanding the dynamic File Name, Subject Name and Email body for a receiver Mail Adapter.

 

Regards,

Karthik

 

 

Original Article:
https://blogs.sap.com/2020/03/30/sending-an-email-attachment-with-dynamic-file-name-and-custom-email-body/

ASK SAP EXPERTS ONLINE
Related blogs

LEAVE A REPLY

Please enter your comment!
Please enter your name here