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
Hello,
I try this solution and it works fine with xml as attachment. But when I use a pdf as attachment the pdf gets corrupted. I use "application/pdf' for the parameter "Attachment.ContentType". I use a simple java mapping.
What is the solution for this?
J.W.M. van der Maarel as mentioned in the restriction section of this blog, it only works XML file attachments.
Alla Rudinskaya as described in this note https://launchpad.support.sap.com/#/notes/2405560 it should be possible using pdf as attachment
Hi J.W.M. van der Maarel
I am having the same issue as you did. PDF attachments are being read from the email, but they are somehow corrupted in SAP PO.
Were you ever able to find a solution? Could you help me with this issue?
Thank you in advance!
Greetings from Vienna,
Nick
Hi Nikiforos,
I have create an incident for SAP on 25.10.2021 and they are still working on it. The last update on the issue was on 10.05.2022:
We are still checking the issue. We will get back to you with our findings.
Unfortunately there is no speed on this incident. As soon as I have more information I will let you know.
Regards,
Hans
Hi Hans,
thanks for your reply.
Let's hope SAP find a solution soon...
Greetings, Nick
Hi Nick,
SAP provided a solution today. SAP-note 3228398. I haven't had a chance to test it yet.
Greetings,
Hans