Skip to Content
Author's profile photo Former Member

Handling attachment as text file with Receiver Mail Adapter with or without mail package

I just share my experience which I got while doing the development to send the payload (Text file) as attachment with SMTP adapter at the receiver communication channel.

This blog explains how the XML to plain text file conversion and attachment name change can be achieved.

The main part explained here is, XML to flat file conversion and payload name change. Please note that this document explains only about the important configurations.

Prerequisites:

  1. Developer should have basic knowledge on PI  ESR and ID developments.
  2. Should have the mail package schema downloaded (if mail package is used for the development)

Here two concepts are explained in this blog

  1. Without mail package type
  2. With mail package type

CONCEPT(A): Without Mail Package Type (Target structure can be any type or compatible type for content conversion)

Image 1-MM.jpg

(Concept: A1). Static Naming for the File (File Conversion using Modules/Beans):

Below is the channel parameters to get the file name “NGC.txt”.

Image 3.jpgImage 2.jpg

Select the “Keep attachments” check box in the receiver communication channel.

Image 4.jpg

Below are the module tab parameters:

1 AF_Modules/StrictXml2PlainBean Local Enterprise Bean    0

2 AF_Modules/PayloadSwapBean Local Enterprise Bean    1

3 AF_Modules/MessageTransformBean  Local Enterprise Bean    2

4 sap.com/com.sap.aii.adapter.mail.app/XIMailAdapterBean Local Enterprise Bean    mail

0 Data.fieldSeparator        ^

0 Header.fieldSeparator  ^

0 Trailer.fieldSeparator    ^

0 recordTypes      Header,Data,Trailer

1 swap.keyName payload-name

1 swap.keyValue      MainDocument

2 Transform.ContentDescription  NGC

2 Transform.ContentDisposition  attachment;filename=NGC.txt

2 Transform.ContentType text/plain

Image 9.jpg

Variables Transport Binding: (Mime type related setting)

XHeaderName1  : Content-Type

XHeaderName2  : Content-Description

XHeaderName3  : Content-Disposition

Additional Parameters:

OMail.AddContentDisposition false

OMail.AddContentDescription false

OMail.AddContentType false

(Concept: A2) Dynamic File Names:

Usually, file name can be changed using java Code (UDF) with  dynamic configuration related API or logic. This file name change works only if we don’t use content conversion.


Note: If we combine dynamic configuration UDF and content conversion modules in the CC, dynamic text file name is not working (File name will be always MainDocument).

Create the below  UDF and assign it to root node of target structure (in Message Mapping):

public String setFilename(String strPrefix, String strSuffixDateFormat, String strFileExtension, Container container) throws StreamTransformationException

{

MappingTrace objTrace = container.getTrace();

DynamicConfiguration objDynConfig;

DynamicConfigurationKey objDCKey;

final String NAMESPACE = “http://sap.com/xi/XI/System/Mail“;

final String ATTRIBUTE3 = “XHeaderName3”; //ContentDisposition

final String ATTRIBUTE1 = “XHeaderName1”;  //ContentType

final String ATTRIBUTE2 = “XHeaderName2”; //ContentDescription

Date objDate = new Date();

String strFomatedDate = “”;

String strFileName = “”;

SimpleDateFormat objSDF = new SimpleDateFormat(strSuffixDateFormat);

try

{

objDynConfig = (DynamicConfiguration) container.getTransformationParameters().

                                get(StreamTransformationConstants.DYNAMIC_CONFIGURATION );

                objDCKey = DynamicConfigurationKey.create(NAMESPACE, ATTRIBUTE3);

                //get the current date and time for file name

strFomatedDate = objSDF.format(objDate);

                //setting the file name dynamically

                strFileName = strPrefix  + strFomatedDate + strFileExtension;

strFileName = “attachment; filename=” + strFileName;

objTrace.addWarning(strFileName);

                objDynConfig.put(objDCKey, strFileName);

strFileName = “text/plain;”;

objDCKey = DynamicConfigurationKey.create(NAMESPACE, ATTRIBUTE1);

objTrace.addWarning(strFileName);

                objDynConfig.put(objDCKey,  strFileName);

strFileName = “NGC”;

objDCKey = DynamicConfigurationKey.create(NAMESPACE, ATTRIBUTE2);

objTrace.addWarning(strFileName);

  • objDynConfig.put(objDCKey, strFileName);
  1. container.setParameter(StreamTransformationConstants.DYNAMIC_CONFIGURATION, objDynConfig);

}

catch (Exception objException)

{

                objTrace.addWarning( objException.getMessage() );

}

return “”;

}

Configure the Receiver channel parameters are as below to enable adapter specific settings:

Select the “Keep attachments” check box in the receiver communication channel.

Image 5.jpg

Image 6.jpg

Module tab parameters:

1              AF_Modules/StrictXml2PlainBean            Local Enterprise Bean    0

2              sap.com/com.sap.aii.adapter.mail.app/XIMailAdapterBean        Local Enterprise Bean    mail

0              Data.fieldSeparator        ^

0              Header.fieldSeparator  ^

0              Trailer.fieldSeparator    ^

0              recordTypes      Header,Data,Trailer

Additional Parameters:

  1. OMail.AddContentDisposition false
  2. OMail.AddContentDescription false
  3. OMail.AddContentType false

So, I would suggest to go for CONCEPT (B) if dynamic file name and content conversion needs to be applied same time…

CONCEPT (B): Target structure should be MAIL package:


Use the mail package external definition file as the target message type (Attached in this blog).

Create an UDF and assign it to target structure  (in Message Mapping):

public String setFilename(String strPrefix, String strSuffixDateFormat, String strFileExtension, Container container) throws StreamTransformationException

{

MappingTrace objTrace = container.getTrace();

DynamicConfiguration objDynConfig;

DynamicConfigurationKey objDCKey;

final String NAMESPACE = “http://sap.com/xi/XI/System/Mail“;

final String ATTRIBUTE3 = “XHeaderName3”; //ContentDisposition

final String ATTRIBUTE1 = “XHeaderName1”;  //ContentType

final String ATTRIBUTE2 = “XHeaderName2”; //ContentDescription

Date objDate = new Date();

String strFomatedDate = “”;

String strFileName = “”;

SimpleDateFormat objSDF = new SimpleDateFormat(strSuffixDateFormat);

try

{

objDynConfig = (DynamicConfiguration) container.getTransformationParameters().

                                get(StreamTransformationConstants.DYNAMIC_CONFIGURATION );

                objDCKey = DynamicConfigurationKey.create(NAMESPACE, ATTRIBUTE3);

                //get the current date and time for file name

strFomatedDate = objSDF.format(objDate);

                //setting the file name dynamically

                strFileName = strPrefix  + strFomatedDate + strFileExtension;

strFileName = “attachment; filename=” + strFileName;

objTrace.addWarning(strFileName);

                objDynConfig.put(objDCKey, strFileName);

strFileName = “text/plain;”;

objDCKey = DynamicConfigurationKey.create(NAMESPACE, ATTRIBUTE1);

objTrace.addWarning(strFileName);

                objDynConfig.put(objDCKey,  strFileName);

strFileName = “NGC”;

objDCKey = DynamicConfigurationKey.create(NAMESPACE, ATTRIBUTE2);

objTrace.addWarning(strFileName);

  • objDynConfig.put(objDCKey, strFileName);
  1. container.setParameter(StreamTransformationConstants.DYNAMIC_CONFIGURATION, objDynConfig);

}

catch (Exception objException)

{

                objTrace.addWarning( objException.getMessage() );

}

return “”;

}

Assign the file name UDF on the root node of target message (in Message Mapping):


Image 7.jpg

Here, prepare the flat file records and assign fill it to content fled of message (separated with the character ‘\n’ for the new line).

Configure the Receiver channel parameters are as below to enable adapter specific settings:

Select the “Use Mail Package” and “Keep attachments” check boxes in the receiver communication channel.

Image 8.jpg

Image 9.jpg

Variables Transport Binding: (Mime type related setting)

XHeaderName1  : Content-Type

XHeaderName2  : Content-Description

XHeaderName3  : Content-Disposition

Additional Parameters

OMail.AddContentDisposition    false

OMail.AddContentDescription    false

OMail.AddContentType              false

Assigned Tags

      6 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Great Article!

       

      I am trying to setup something similar. I hope you can provide me with some advice what's wrong here:

       

      i have a following scenario: XML to Mail as csv attachment.

      I have set up the scenario and it works if is use transform.content type as a static file name but when i try to rename it using FIleName from asma nothing happens eventhough the message monitoring says it change the name of the attachment.

      Can someone tell me what is wrong here and how can i fix it. I am on a PO 7.31 single stack

       

       

      Adapter Module Code:

       

       

      package com.sap.adaptermodule.rapportage;

      import java.util.Iterator;

      import java.util.Set;

      import javax.ejb.CreateException;

      import javax.ejb.SessionBean;

      import javax.ejb.SessionContext;

      import com.sap.aii.af.lib.mp.module.Module;

      import com.sap.aii.af.lib.mp.module.ModuleContext;

      import com.sap.aii.af.lib.mp.module.ModuleData;

      import com.sap.aii.af.lib.mp.module.ModuleException;

      import com.sap.engine.interfaces.messaging.api.Message;

      import com.sap.engine.interfaces.messaging.api.MessageKey;

      import com.sap.engine.interfaces.messaging.api.MessagePropertyKey;

      import com.sap.engine.interfaces.messaging.api.Payload;

      import com.sap.engine.interfaces.messaging.api.PublicAPIAccessFactory;

      import com.sap.engine.interfaces.messaging.api.auditlog.AuditAccess;

      import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;

      import com.sap.tc.logging.Location;

      public class SetAttachmentNameBean implements SessionBean, Module {

       

          private SessionContext myContext;

       

          public void ejbRemove() {

          }

          public void ejbActivate() {

          }

          public void ejbPassivate() {

          }

          public void setSessionContext(SessionContext context) {

              myContext = context;

          }

          public void ejbCreate() throws CreateException {

          }

       

      public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData)

                            throws ModuleException{

        String SIGNATURE = "process(ModuleContext moduleContext, ModuleData inputModuleData)";

        Location location = null;

        AuditAccess audit = null;

        MessageKey key = null;

       

        try {

          

        location = Location.getLocation(this.getClass().getName());

       

              Message msg = (Message) inputModuleData.getPrincipalData();

        key = new MessageKey(msg.getMessageId(), msg.getMessageDirection());

        audit = PublicAPIAccessFactory.getPublicAPIAccess()

        .getAuditAccess();// creating object for audit log

             

              Payload payload = msg.getDocument();

              Set<MessagePropertyKey> mpkSet = msg.getMessagePropertyKeys();

              Iterator<MessagePropertyKey> mpkIterator = mpkSet.iterator();

              MessagePropertyKey mpk = null;

              for (;mpkIterator.hasNext();){

              mpk = mpkIterator.next();

              audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, "Message Property gevonden met Name: " + mpk.getPropertyName());

              if ("FileName".equalsIgnoreCase(mpk.getPropertyName())){

              break;

              }

            

              }

             

              audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, "Payload gegevens initieel, Name: " + payload.getName()+

              ", Description: " + payload.getDescription() + ", ContentType: " + payload.getContentType());

             

              String fileName = msg.getMessageProperty(new MessagePropertyKey("FileName", "http://sap.com/xi/XI/System/File"));

       

              if(fileName == null) fileName="default.txt";

       

              payload.setContentType("text/plain;charset = \"UTF-8\";" + "name=\"" + fileName + "\"");

           

             

              audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, "Payload gegevens na setten, Name: " + payload.getName()+

              ", Description: " + payload.getDescription() + ", ContentType: " + payload.getContentType());

       

              inputModuleData.setPrincipalData(msg);

       

          } catch (Exception e) {

              throw new ModuleException(e);

          }

       

          return inputModuleData;

      }

      }

       

       

       

      This is my Mail adapter configuration:

      https://www.dropbox.com/s/7gp9n0fdwnxhf84/27-10-2014%209-15-53.jpg?dl=0

      https://www.dropbox.com/s/e2b2wi4uegfdflc/27-10-2014%209-16-29.jpg?dl=0

      https://www.dropbox.com/s/fj46pzkzsnkjc9x/27-10-2014%209-17-19.jpg?dl=0

       

       

       

      This is what message monitoring says:

      https://www.dropbox.com/s/vzz04n3i2fhaivr/27-10-2014%209-14-08.jpg?dl=0

      https://www.dropbox.com/s/9opsgz3dsny3uam/27-10-2014%209-30-25.jpg?dl=0

      https://www.dropbox.com/s/gtws1hgfr7f4l8b/27-10-2014%209-30-55.jpg?dl=0

       

       

       

      And this is my mail result

      https://www.dropbox.com/s/v0gh9my8eulzalm/27-10-2014%2010-08-47.jpg?dl=0

      I want the Attachment.xml to be renamed with ASMA FileName.

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      In the "Advanced" Tab,  select the check box "Variable Transport Binding" and fill the variable header fields as I mentioned in the blog....

       

      And also, select the check box "Advance Mode", and provide the Additional parameters as I mentioned in the blog..

       

      This might work...

      Author's profile photo Otto Frost
      Otto Frost

      I had the same issue.
      I tried everything in this post from before but it doesn’t work on 7.4 SP12.

      How to do it

      transport protocol = SMTP
      message protocol = XIPAYLOAD

      on general tab in mail receiver channel
      content encoding = base64
      keep attachments = X
      on advanced tab
      use asma = X
      variabel transport binding = X

      we use an adapter module that allows java script in the module configuration, but you can develop a java adapter module doing the same

      globals0001.js

      var mCtx = scriptHelper.getModuleContext();

      var iMD = scriptHelper.getModuleData();

      var msg = scriptHelper.getMessage();

      var audit = scriptHelper.getAuditAccess();

      var mk = scriptHelper.getMessageKey();

      var location = scriptHelper.getLocation();

      the email will then look like this

      To: to@to.se
      From: from@from.com
      MIME-Version: 1.0
      Content-ID: <payload-2af29c35c82211e69b0c000032cc2ee7@sap.com>
      content-disposition: attachment;filename=”fname.xml”
      Content-Type: application/xml; charset=”utf-8″
      Content-Description: fname.xml
      Content-Transfer-Encoding: base64

      PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjxyc206VmFsaWRhdGVkRGF0
      more base64 stuff
      ZW1lbnRGb3JBZ2dyZWdhdG9yPg==

      Author's profile photo Otto Frost
      Otto Frost

      Is there a sap note about

      OMail.AddContentDisposition false

      OMail.AddContentDescription false

      OMail.AddContentType false

      describing them

      or maybe some documentation elsewhere?

      Author's profile photo Ambrish Mishra
      Ambrish Mishra

      I tried the above scenario and it doesn't work in PO 7.5. Is there something missing in the product to enable this functionality?

      Author's profile photo Sandra Hagmann
      Sandra Hagmann

      Hello,

      I use SAP PO 7.50

      I have also tried to use a Mail channel with Content-Disposition and Content-Type. But the Content-Typ at the mime Header of the mail is always the wrong. The Content-Disposition, I set via UDF is the correct one.

       

      MIME Header of the mail:

      Content-Type: text/plain;charset="UTF-8";attachment;filename="Order_4510090920.xls"
      Content-Disposition: attachment; filename=Order_4510090920.xls
      Content-Description: XML

       

      I set the Content-Type in the mapping with UDF.

       

      String compositeContentType = contentType + "\";attachment;filename=\"" + fileName + "\"";
      String compositeDisposition = "attachment; filename=" + fileName;
      String compositeDescription = fileName;
      container.getOutputHeader().setContentType(compositeContentType);

      DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
      if (conf != null) {
      DynamicConfigurationKey keyX1 = DynamicConfigurationKey.create("http:/" + "/sap.com/xi/XI/System/Mail", "XHeaderName1");
      conf.put(keyX1, compositeDisposition);
      DynamicConfigurationKey keyX2 = DynamicConfigurationKey.create("http:/" + "/sap.com/xi/XI/System/Mail", "XHeaderName2");
      conf.put(keyX2, compositeContentType);
      DynamicConfigurationKey keyX3 = DynamicConfigurationKey.create("http:/" + "/sap.com/xi/XI/System/Mail", "XHeaderName3");
      conf.put(keyX3, compositeDescription);
      }

       

      The dynamic variables are showed correctly at the message log:

      <sap:Record namespace="http://sap.com/xi/XI/System/Mail" name="XHeaderName2">application/msexcel";attachment;filename="Order_4510090920.xls"</sap:Record>

      <sap:Record namespace="http://sap.com/xi/XI/System/Mail" name="XHeaderName3">Order_4510090920.xls</sap:Record>

      <sap:Record namespace="http://sap.com/xi/XI/System/Mail" name="XHeaderName1">attachment; filename=Order_4510090920.xls</sap:Record>

       

      When I additionally include the module MessageTransformBean

      Transform.ContentType, value application/msexcel

      then the Content typ at the mime Header of my mail is correct.

       

      But how could I get the header variables to the mime Header?

       

      Thanks in advance for any help

      Sandra