Skip to Content
Technical Articles

Preserving Attachment File Names in a Mail to File scenario with Multiple Attachments

Introduction:

With the help of the SAP Note# 2040884, now we can process multiple mail attachments as a separate XI message in SAP PI IS/ AAE. In this article I will share how to preserve the file name of each attachment and dynamically set that name as a target file name.

Scenario:

We will take a simple scenario of Mail to File where the sender mail adapter will pick a mail with multiple attachments. In this case we would like to process only the attachments and not the main payload (Mail Body and Headers). Considering the mail attachments will be processed into the same target directory, for the simplicity of the scenario.

Configuration,setting and implementation:

For this particular requirement we have setup the adapter module of  our sender mail adapter channel as below, according to the SAP Note mentioned above-

This will treat the incoming message as below –

If the mail message contains 5 XML attachments then 5 XI messages will be created and the main payload will not create any XI message.

After setting this up and configuring the end to end scenario with the receiver file adapter if we run the scenario it will create 5 Files from each of the attachment at target server.

Now we need to preserve the name from attachment and assign that to respective file created at target server.

If we monitor the message IDs created after the mail is picked up and sent to PI, the Mail attachment names are not available at any attributes or logs.

But, if we open the entire SOAP message from message monitoring we can see the attachment file name is there as Content-Type-

We will be reading this content type from from the message and extract the file name from there. After the file name is retrieved we can assign that to the target file name using Dynamic Configuration in the map.

Mapping Code:

Two different Mapping techniques can be used for this requirement based on the type of the scenario.

If the scenario involves transformation from source to target, then we can achieve this using a UDF in the graphical mapping. Below UDF can be assigned to the root node of the target structure.

Please take a close look on the content type populated, as based on that the UDF might need some modifications.

UDF Code:

String  cont;
java.util.Map map;
map = container.getTransformationParameters();
cont = (String) map.get(StreamTransformationConstants.CONTENT_TYPE);
int i = cont.lastIndexOf("=");
cont = cont.substring(i+1);
cont = cont.replace("\"","");
DynamicConfiguration conf1 = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
DynamicConfigurationKey key1 = DynamicConfigurationKey.create( "http:/"+"/sap.com/xi/XI/System/File","FileName");
conf1.put(key1,cont);
return "";

 

If the scenario is a pass through and no mapping is involved then, a simple java mapping can be used to get the attachment name and set the file name with that with the same approach as UDF. And passing the input stream as it is for output stream.

Testing:

For testing the scenario, we have received an email with attachments as below –

 

 

 

 

When checked the PI sender mail adapter it will create 3 Message IDs and skip the main payload-

 

Each Message ID will have their respective mail attachment name at SOAP message under the message content tab –

If we check the Dynamic Configuration of the message at this point, we will see the target file name is set as the source mail attachment name –

As a last step, if we check the target FTP server, the file names will be created as the same name from the mail attachments.

 

Log from the receiver channel –

 

Remarks:

We can also achieve the same requirement, if we have different structures for attachments. In that case we need to create multiple mappings and determine the mapping using XPath of the source structures.

If we also want to process the main payload then we can set the Mail.IgnoreMainPayload parameter as false. And then create another mapping and select the mapping based on the XPath.

In case of different target directories, we can add a Dynamic Configuration code in the map to set target directories at runtime based on the XPath as well.

Restriction:

This new SAP feature will only consider the XML attachments, any other attachments (e.g .txt, .XLS etc.) will be ignored and no message IDs will be created.

Resources:

StreamTransformationConstants – https://help.sap.com/doc/2f39047ed6b141cb83658041d2d4e029/7.5.16/en-US/PI/index.html

Processing Mail attachments as PI messages (SMP Login Required) – https://launchpad.support.sap.com/#/notes/2040884

Dynamic Directory for target server- https://launchpad.support.sap.com/#/notes/2441689

New Feature: Dynamic directory configuration for archive and error archive directory in File adapter-

https://launchpad.support.sap.com/#/notes/2502789

 

Be the first to leave a comment
You must be Logged on to comment or reply to a post.