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.