Skip to Content

Update March 28, 2014:

I think the mail receiver adapter will soon be supporting dynamic filename, see my comment from today.

Recently I had a requirement in SAP PI to dynamically set filename of an email attachment on a mail receiver channel (outgoing message). I already knew this could be achieved by means of “Use Mail Package” option which is available when using message protocol XIPAYLOAD.

But is “Use Mail Package” really necessary ? Unfortunately the mail adapter does not provide an Adapter-Specific Message Attribute for setting Content-Type or Content-Disposition.

My next thought was to use MessageTransformBean which can indeed set attachment filename using transform.ContentDisposition or transform.ContentType – but only as static values, no support for dynamic properties. What a shame.

Finally I checked the SAP NW PI javadocs and found just what I was looking for ( works for 7.1x and 7.3x ):

http://help.sap.com/javadocs/NW73EHP1/SPS01/PI/index.html?com/sap/aii/mapping/api/OutputHeader.html

So in a message mapping all you have to do is write a small UDF that uses method setContentType in a way where you set not only the Content-Type, but also charset and … ta da… filename!

For instance a single values UDF with three string arguments ContentType, CharSet and FileName:

/*
Set Content-Type in output header, for instance to text/xml;charset=”UTF-8″;filename=”INVOICE_00000012114_00110514008.xml”

If the message is sent to a mail receiver channel the Content-Type in the output header will be used.

This way we can set a dynamic mail attachment filename without using mail package structure.
*/

String CompositeContentType = ContentType + “;charset=\”” + CharSet + “\”;filename=\”” + FileName + “\””;

container.getOutputHeader().setContentType(CompositeContentType);

return CompositeContentType;

You would typically use this UDF on a mapping variable ( “Add variable” ) in your target message.

Or in a pure java mapping:

public void transform(TransformationInput arg0, TransformationOutput arg1)

.

.

.

arg1.getOutputHeader().setContentType(CompositeContentType);

I prefer this rather than “Use Mail Package” option. Especially if some kind of twist or tweak needs to be done. For instance if you want to apply one or more (standard or custom) modules to the module chain of your mail channel to further manipulate your payload to make it ready for attachment. Then you would often be better off not having to deal with the extra level of complexity that the Mail Package brings.

regards

Mauri

To report this post you need to login first.

25 Comments

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

    1. Mikael Mauri Frandsen Post author

      Hi Krish,

      I really have not made any ABAP mappings of my own, so I do not know if Content-Type can be set in this type of mapping. But of course you can always add a java mapping ( or 1:1 message mapping with the mentioned UDF ) to your operation mapping to set it.

      regards

      Mauri

      (0) 
      1. Krish Gopalan

        Hi Mauri

        I was able to set the content type in ABAP mapping.. When I did that my payload showed up with the new content type. Unfortunately the attachment name was like adfndf.dat (arbitrary characters).

        Do you know if this set up works in 7.1? Did you use any modules? Also did you require email with body and attachment or just the attachment?

        regards

        Krish

        (0) 
  1. dhinesh kumar raj kumar

    Hi Mauri,

    I am working on a file to mail scenario, i have to set the name of the attachments dynamically as the same name of the files I am sending …so whether i have to use a java mapping or is there any module avilable for achiving this …can you please help me to resolve it.

    I used the following java Mapping but,it s not setting the name dynamically

    public class Dfilename extends AbstractTransformation {

    public void transform(TransformationInput in, TransformationOutput out)

    throws StreamTransformationException {

      // TODO Auto-generated method stub

      Collection<String> contentid=null;

      if(in.getInputAttachments().areAttachmentsAvailable())

      {

       contentid=in.getInputAttachments().getAllContentIds(true);

      }

      for(String id:contentid)

      {

       Attachment attachment=in.getInputAttachments().getAttachment(id);

       byte[] content=attachment.getContent();

       String contenttype=attachment.getContentType();

      

       out.getOutputAttachments().setAttachment(out.getOutputAttachments().create(id, contenttype, content));

     

    out.getOutputHeader().setContentType(contenttype);

      

      }

     

     

    }  }

    (0) 
  2. Praveen Kurni

    Hi Mauri,

    Thanks for sharing valuable information with a nice blog.

    I tried to set contentType using the UDF, it worked successfully in IE, But in my scenario I had to use module StrictXML2PlainBean to convert XML to text. But Adapter Engine is sending mail with default attachment name (MainDocument.txt).

    In my mapping I have provided the parameter value for File Name as “.txt”, still AE is unable to set the attachment name as provided in mapping.

    Q: Does the contentType set in UDF is defaulted in AE, though I set the ContentType to “text/plain.

    Appreciate your reply.

    (0) 
    1. Rams G

      Dear Thomas,

      I do have the same requirement. There will be different kind of files (.txt/.img/.pdf) on FTP. PI has to pick them and send to the users as an attachment. The file name and type shouldn’t be changed. We should send same file name as an attachment.

      I have created the UDF and mapped to the target node (Screenshot below). still I am getting the attachment as Untitled.xml.

      Could you please help me how you have succeed in this case?

      DyConfig1.png

      DyConfig2.png

      Thanks-

      Ramesh.

      (0) 
      1. Thomas Brommont

        Hi Ramesh,

        with fast view you have the UDF mapped to the root. Thats wrong. See answer to Vimal. Use a variable in the mapping.
        Also do not use  >  ”  <

        Regards

        Thomas

        (0) 
        1. Rams G

          Thanks Thomas, I have modified as per your comments.

          Now i am getting following error on receiver communication channel.

          Message processing failed. Cause: com.sap.aii.af.sdk.xi.srt.BubbleException: Failed to call the endpoint  [null “null”]; nested exception caused by: com.sap.aii.af.sdk.xi.util.XMLScanException: expecting start tag: {http://sap.com/xi/XI/Mail/30}Mail, but found {http://sidel.com/xi/Mail/Test}MT_Mail_RCV at state 1

          Just for information, i have done the following configuration…

          1. Selected the ‘File Name’ option under Adapter-Specific Message attributes on Sender File Adapter

          2. Selected ‘Use Adapter-Specific message attributes’ on receiver mail adapter.

          Please let me know if i am doing any mistake here…

          Thanks-
          Ramesh.

          (0) 
        2. Rams G

          Hi Thomas,

          I have modified as per your suggestions and now all objects are ok. However I am not receiving the source file name on attachment instead it is Untitled.xml.

          Could you please help me how to get the source file name on attachment now?

          Thanks-
          Ramesh

          (0) 
  3. Simon Stehle

    Hi Mauri,

    we want to use your solution. But we are not experts, we did not know where to insert this coding exactly in the area Message Mapping.

    Simon

    (0) 
  4. Simon Stehle

    Hi Thomas,

    thanks! It runs. I assigned this function to to IDOC @Begin Tag and the file name is now the invoice number.

    Is there a possability that the file ATT00001.xml will not be generateda and assigned to the mail?

    Simon

    (0) 
    1. Mikael Mauri Frandsen Post author

      Hi Simon – a few days ago I have seen the exact same problem with attachment name showing as ATT00001.xml.

      When I made my solution for the customer a year ago everything worked as expected. Then maybe half a year later the attachments suddenly started to be named ATT00001.xml. I was not aware of that at the time – I learned it last week – as nobody seemed to care too much about it.

      I know for sure that nothing was changed on PI. But one thing has changed. The customer changed mail system from Notes to Outlook. So I assume that the smtp server used by the mail receiver channel is now another one. It seems that the new smtp server interprets the request from mail receiver channel in another way than the old one. It probably fails to see the filename given in content-type and therefore it generates its own name ATT00001.xml.

      I just googled ATT00001, it seems to be a known issue linked to Microsoft Exchange server. But why that happens I can not say as I have not found an explanation. I might be there though, if one keeps looking…

      I have checked the headers from Outlook: Open the mail, then File / Properties (large button), then under Internet Headers you can scroll down and ffind Content-Type. It has exactly the expected value as set in the UDF.

      When I find some time I will try to look into this issue.

      regards

      Mauri

      (0) 
      1. Vimal Shah

        Hi Mauri,

        I copied the above udf and assigned it to a variable fileattr, now what do I do?

        Meaning how to configure in ID now, so that I will get the dynamic file name.

        Please help me

        Regard,

        Vimal

        (0) 
    2. Vimal Shah

      Hi Simon,

      I got the code in the mapping to a variable.  How do I configure this at ID now?

      Please help me.  Or I am missing something in the IR?

      (0) 
      1. Thomas Brommont

        Hi Vimal,

        set in the target message the variable “FileName”. I think your problem is to set the variable “fileattr” but this is the name of the UDF, not from the parameter.

        Use the UDF “fileattr” in the mapping tool to set the content of the variable “FileName.”

        Activate in the config from the mail receiver adapter (“Advanced”) “Use Adapter-Specific Message Attributes”

        Good Luck

        Thomas

        (0) 
  5. Mikael Mauri Frandsen Post author

    Hi everybody,

    a couple of months ago I mentioned that I, like some of you, have experienced that setting content-type as shown in my blog does not always give the result we want. For instance we have learned that Microsoft Exchange server – at least valid for newer versions – does not seem to set attachment filename based on Filename in Content-Type. Instead a filename like ATT00001 is generated. For other mail servers, such as Lotus Domino/Notes it seems to work as intended.

    But I think I have some promising news on this topic.

    When browsing through patch texts for software component MESSAGING I came across this piece of information that indicates the mail receiver adapter will soon be supporting dynamic filename for attachment.

    Dynamic filename in MAIL receiver adapter.JPG

    This information is valid for these versions:

    MESSAGING 7.31 SP10 patch 11 (MESSAGING10_11-20007081.SCA)

    MESSAGING 7.40 SP05 patch 9 (MESSAGING05_9-20009954.SCA)

    The referenced SAP note 1990598 is not yet released, so it just says “Document is not released” when you try to open it. You are welcome to check for yourself if this has changed ( SMP user required ):

    http://service.sap.com/sap/support/notes/1990598

    I also checked on-line help at help.sap.com and looked for updated SAP_BASIS content ( has the adapter metadata ), but I found nothing yet.

    If/when I find some useful news I will let you know. Or if anybody else knows something, please share.

    regards

    Mauri

    (0) 
  6. Thomas Brommont

    Hi Mauri,

    I’ve tested a few configurations. If you sent from PI without messagebody and the Exchange add the disclaimer some issues are possible on receiver site (mail in mail, mail with eml attachment, mail with rtf attachment).

    On Receiver mail Client the issue is always the same.

    Following can solve this issue on PI site:

    – sent with a Sender address which the Exchange don’ t add a disclaimer

    – make a messagebody in the mail

    or shut down the disclaimer on Exchange and make a disclaimer in SAP.

    Maybe is the same issue with AT…

    Tested with 7.11

    KR Thomas

    (0) 
    1. Krish Gopalan

      Hi Thomas / Mauri,

      We have the Dynamic email body and dynamic attachment name .. and to make it worse we are actually swapping payloads.

      We achieved this by setting the XHeaderName1(variable transport binding) in mapping and Payload swap bean in module parameters. We didn’t set the content type as per the blog (and i am going to try that now in addition to content disposition). We are on the latest support packs with the note mentioned by Mauri implemented.

      It works perfectly for some mail clients. We are having issues with other clients (outlook being one of them).

      a) swapping does not work.. Email Body becomes attachment to mail and vice versa

      b) attachment is actually stripped in some emails

      c) both body and attachment are received as attachments.

      Krish

      (0) 
      1. Krish Gopalan

        Okay.. We were getting two attachments in microsoft (one attachment had dynamic name and other had that ATT* name)…

        We changed the module parameter mime.contenttype to ‘multipart/mixed’ and it appears to work for outlook.

        (0) 
  7. Doug Munford

    Microsoft Exchange (perhaps later versions) now fails to respond to filename as part of ContentType, the other parts of ContentType still work ok (so I can change to text/plain for example), but the answer for filename is to place it in ContentDisposition.

    ContentType=text/xml;charset=”utf-8″
    ContentDisposition=attachment;filename=”myfilename.xml”

    I am using the AF_Modules/MessageTransformBean and just came across this problem with a change of destination Email server from config that was working last year.

    Doug.

    (0) 

Leave a Reply