Skip to Content
Author's profile photo Former Member

XI Mail Adapter : Dynamically building attachment and message body content using a simple UDF

We came across a requirement at our project where we have to create message body content as well as the attachment content dynamically in the mapping.


A search on SDN threw up XI Mail Adapter: An approach for sending emails with attachment with help of Java mapping.


In the comments on the article Nasim Kurmoo has given the code listing for a UDF that can be used to get a similar result. 


But the comment doesn’t describe how the UDF can be implemented, nor does it justify how useful that code is. So I thought I’d post (as an example) how we implemented a similar UDF for our requirement.


Given a set of failure records, our requirement was to send a summary note of the failure (i.e. count of the number of failure records) in the message body and to include the details of the failure records as the attachment.


The source listing of the UDF is as follows :


The content type should be set as multipart/mixed and the boundary value should also be specified.


The boundary value will identify where one part (of the multipart content) ends and the next one begins.




In order to build the content of the attachment (for this particular requirement) we use a global variable declared and initialized in the global sections :





The function addToAttachment is used to add each Invalid Record to the globally declared string messageAttachment.




The function addToBody will insert the calculated value (count of total Invalid Records) into some custom Message Content.





The following sample mail is generated by this implementation :





Note : Don’t forget to set the Content Encoding as None in the Mail attributes section of the mail adapter.

Assigned Tags

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

      Hi I'm trying to implement the above scenario in SAP PI 7.31 single stack system however I'm facing issues the below issues any help is highly appreciated.

      the output of mail package mapping mapping is as follows:

      <?xml version="1.0" encoding="UTF-8"?>

      <ns1:Mail xmlns:ns1="">

         <Subject>Mail Excel attachment package</Subject>



         <Content_Type>multipart/mixed; boundary=--AaZz</Content_Type>


      <Content><![CDATA[----AaZz Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline  Hi, This ia a test email. ----AaZz Content-Type: application/xml; name=Invoice.txt Content-Disposition: attachment; filename=Invoice.txt  test email ]]></Content>


      This contains all the required information. However this is not working any thoughts?