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
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
}
}
}
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 a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
7 | |
5 | |
5 | |
5 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 |