Skip to Content
Technical Articles

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.


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.


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

Below is Java Code:

package Mail;

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.
 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("", "FileName"); //creating a Dynamic Configuration key to get the Input File FileName
   DynamicConfigurationKey subkey = DynamicConfigurationKey.create("", "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; //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:


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.






You must be Logged on to comment or reply to a post.
    • Hi Evgeniy


      I am new in PI/PO and have a requirement of reading a file from SFTP and send it as email to fixed receivers with SUBJECT being name of the FILE.

      Is it possible to achieve this without ESR Objects and Java Mapping? I tried by using ASMA on sender CC and "DynamicConfiguration" on Receiver MAIL ADAPTER side.


      Receiver Mail Adapter Module Configuration

      Receiver Mail Adapter Module Config


      ASMA settings



      Mail Attributes


      The file is being picked up, email sent with attachment but SUBJECT LINE coming as BLANK.


      Can you please guide me on what am I doing wrong? Or IT is NOT POSSIBLE to set SUBJECT Dynamically without ESR & UDF?


      Appreciate your help/guidence.


      Thank you,


      • Hi Lakshmi!

        I would try to use the following sequence for DynamicConfigurationBean module:


        key.0 write FileName

        value.0 message.correlationId

        key.1 read THeaderSUBJECT

        value.1 message.correlationId


        Regards, Evgeniy.

        • Thank you So much Evgeniy 🙂


          Yes it worked perfectly.

          If we need to do any prefix concatenation (Ex: ChainID Report : AttachmentFileName) we need to use UDF in ESR. Is that correct?

          Or can we do anything like we do in File Adapter?    there we do like CHAINIDREPORT: %filename% TO make it concatenated

          Once again thank you for quick guidance which in-fact solved my requirement.


          • Hi Lakshmi!

            It depends on your requirement. You can use "Variable Substitution" functionality in File adapter. There you can use string constant parts, message headers and payload values to build the result file name. If this functionality is not sufficient to fulfill your requirement - you should use ESR objects.

            Regards, Evgeniy.

  • Hi Karthik,

    I have the same requirement, followed the instructions as it is. But i am getting below error:

    Transmitting the message to endpoint <local> using connection File_ failed, due to: Mapping failed in runtimeApplication mapping program com/emailmapping/Emailmapping throws a stream transformation exception: Error in java mapping:The input-parameter SubjectName does not exist.

    May i know how to fix it. Please help if you have any screenshots.



  • Hi Sham,

    From the error “The input-parameter SubjectName does not exist” have not created any Subject name parameter in ESR , remove that in Java code i.e. “String myparameter” one and check it.



    • Hi Karthik,

      It failing with SWCV error. Unable to find resource <> in the following software component versions. Could you please share the screenshots from your configurations, what I am missing in ESR?

      That will be helping me to resolve the issue.

      Objects I created are:

      1. One message type for source and target
      2. Operation Mapping (With JAVA mapping)

      It is highly critical issue for us now. Your input will help us.

      Thank you.