Skip to Content

Recently I worked on a scenario where the requirement was to send email with HTML content in body and source file as an attachment.

HTML content can also be generated with XSLT mapping along with Message/Java Mapping. But why to use multimapping, when we can get the result in one mapping.

For this scenario, I used only Java mapping to generate the Mail Package structure with the HTML content, styled with CSS.

CSS helps to describes how HTML elements are to be displayed.

Here, I have used internal CSS to format the mail body as it is easier to demonstrate the style.

Lets refer to the below Java code.

Java Mapping to create Mail Package Structure :

public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException



String inputdata = in.getInputPayload().getInputStream();

mailSubject = “Sample Email with attachment”;
attachmentName = File_Name;                           //filename can be populated dynamically from message header

mailContent = “<html>” + CRLF
+”<head>” + CRLF
+”<title>Sample Email with Attachment</title>” + CRLF
+”<style type=\”text/css\”>” + CRLF
+”body {” + CRLF
+” background-color: white;” + CRLF
+” font-family: arial, sans-serif;” + CRLF
+” font-size: 100%;” + CRLF
+” margin-top: 10px;” + CRLF
+” margin-right: 20px;” + CRLF
+” margin-bottom: 10px;” + CRLF
+” margin-left: 20px;” + CRLF
+” padding: 0;” + CRLF
+”}” + CRLF

+”div#banner {” + CRLF
+” margin: 0;” + CRLF
+” margin-bottom: 10px;” + CRLF
+” color: #000;” + CRLF
+” background: #cce6ff;” + CRLF
+” border: 1px solid black;” + CRLF
+” padding-top: 5px;” + CRLF
+” padding-bottom: 5px;” + CRLF
+” padding-left: 20px;” + CRLF
+” padding-right: 20px;” + CRLF
+”}” + CRLF

+”.right {” + CRLF
+” text-align: right;” + CRLF
+”}” + CRLF

+”</style>” + CRLF
+”</head>” + CRLF
+”<body>” + CRLF
+”<h1>”+”TEST EMAIL”+”</h1>”+ CRLF
+”<div id = \”banner\”>” + CRLF

+”<b>DATE: </b>” +Date+ CRLF                                               //Can be populated dynamically
+”<br/><b>Message ID: </b>” +msgID+ CRLF

+”<br/><br/>Please see the attachment for original File.” + CRLF

+”</div>” + CRLF
+”</body>” + CRLF
+”</html>” + CRLF;

//create XML structure of mail package

String output = “<?xml version=\”1.0\” encoding=\”UTF-8\”?>”
+ “<ns:Mail xmlns:ns=\”\”>”
+ “<Subject>” + Mail Subject + “</Subject>”
+ “<From>” + sender email ID + “</From>”
+ “<To>” + Receiver email ID + “</To>”
+ “<Content_Type>multipart/mixed; boundary=\”” + boundary + “\”</Content_Type>”
+ “<Content>”;


// create the declaration of the MIME parts

//First part. It will contain the HTML content

output = “–” + boundary + CRLF
+ “Content-Type: text/html; charset=UTF-8” + CRLF
+ “Content-Disposition: inline” + CRLF + CRLF
+ mailContent + CRLF

//Second part. It contains data to be sent as an attachment.

+ “–” + boundary + CRLF
+ “Content-Type: application/xml; name=” + attachmentName + CRLF
+ “Content-Disposition: attachment; filename=” + attachmentName + CRLF + CRLF;

copySource(inputdata, out.getOutputPayload().getOutputStream());


catch (Exception e){}


protected static void copySource(InputStream in, OutputStream out) throws IOException, StreamTransformationException

int c;

while ((c = != -1){
out.write(c); }


catch(Exception e){}



On implementing the Java mapping, the below mail package structure is generated as XML message. The output contains multiple parts where the first part is HTML text with CSS style and the second part is data from source payload which is sent as an attachment.

Also, please remember to select Use Mail Package, Content Encoding as ‘None’ and Keep Attachments as checked in the receiver communication channel.

Mail Package Structure :

<?xml version=”1.0″ encoding=”UTF-8″?><ns:Mail xmlns:ns=”″>
<Subject>Sample Email with attachment</Subject>
<Content_Type>multipart/mixed; boundary=”–AaZz”</Content_Type>
Content-Type: text/html; charset=UTF-8
Content-Disposition: inline<html>
<title>Sample Email with Attachment</title>
<style type=”text/css”>
body {
background-color: white;
font-family: arial, sans-serif;
font-size: 100%;
margin-top: 10px;
margin-right: 20px;
margin-bottom: 10px;
margin-left: 20px;
padding: 0;
div#banner {
margin: 0;
margin-bottom: 10px;
color: #000;
background: #cce6ff;
border: 1px solid black;
padding-top: 5px;
padding-bottom: 5px;
padding-left: 20px;
padding-right: 20px;
.right {
text-align: right;
<h1>TEST EMAIL</h1>
<div id = “banner”>
<b>DATE: </b>20171129T231100Z
<br/><b>Message ID: </b>f82b3c5c-cb1a-11e7-8fbe-0000182374d2
<br/><br/>Please see the attachment for original File.</div>
Content-Type: application/xml; name=Test_File.edi
Content-Disposition: attachment; filename=Test_File.ediUNA:+.? ‘UNB+UNOC:3+1111111111111:14+5790000075003:14+150601:1528+V434687+++++EANCOM’UNH+77+INVOIC:D:01B:UN:EAN010′
DTM+ZPE:20150601:102’FTX+ZZZ+1+ST2+Rahmen- und Kond:itionsvereinbarungen.+DE’
MOA+8:100.57’MOA+25:8522.85’LIN+1++3073781054132:SRV’PIA+1+00433482:SA::91’IMD+A++:::LRD MIX3 225/260G X 20 STD’


HTML Email :


NOTE: This approach only works with non binary attachments.

To report this post you need to login first.

1 Comment

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

  1. Emma Sherman

    Wow! What a fantastic article! Many thanks for detailed description of your point of view and for interesting details. I also appreciate, that you provide here your solution and codes, that will be helpful during working with my own website. Best regards, Emma



Leave a Reply