Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
bobnayak
Explorer
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

{

try{

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=\"http://sap.com/xi/XI/Mail/30\">"
+ "<Subject>" + Mail Subject + "</Subject>"
+ "<From>" + sender email ID + "</From>"
+ "<To>" + Receiver email ID + "</To>"
+ "<Content_Type>multipart/mixed; boundary=\"" + boundary + "\"</Content_Type>"
+ "<Content>";

out.getOutputPayload().getOutputStream().write(output.getBytes());

// 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;

out.getOutputPayload().getOutputStream().write(output.getBytes());
copySource(inputdata, out.getOutputPayload().getOutputStream());
out.getOutputPayload().getOutputStream().write("</Content></ns:Mail>".getBytes());

}

catch (Exception e){}

}

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

int c;

try{
while ((c = in.read()) != -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="http://sap.com/xi/XI/Mail/30">
<Subject>Sample Email with attachment</Subject>
<From>bobby@xyz.com</From><To>bobby@xyz.com</To>
<Content_Type>multipart/mixed; boundary="--AaZz"</Content_Type>
<Content>----AaZz
Content-Type: text/html; charset=UTF-8
Content-Disposition: inline<html>
<head>
<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;
}
</style>
</head>
<body>
<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>
</body>
</html>----AaZz
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'
BGM+380+2557570672+9'DTM+137:20150601:102'DTM+35:20150601:102'DTM+999:20150601:102'DTM+69:20150601:102'
DTM+ZPE:20150601:102'FTX+ZZZ+1+ST2+Rahmen- und Kond:itionsvereinbarungen.+DE'
RFF+CT:20'RFF+EZ1:E60'RFF+ACE:0141188911'DTM+171:20150601:102'RFF+DQ:0082417930'
DTM+171:20150601:102'RFF+ON:4004801616'RFF+998:506'RFF+ABO:3500291332'DTM+171:20150601:102'
NAD+BY+4314799000001::9'RFF+VA:DE291493807'RFF+ADE:799000'NAD+SU+4311501000008::9'RFF+VA:DE811135425'
RFF+FC:27/254/00074'RFF+ADE:291560'NAD+DS+4042000000006::9'NAD+IV+4311501000008::9'NAD+DP+4314799990012::9'
RFF+ADE:799001'TAX+7+VAT+++:::7'CUX+2:EUR:4'PAT+3'DTM+13:20150625:102'ALC+AI++++E01'PCD+3:1.18'
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'
IMD+C++IN::9'IMD+C++CU::9'QTY+47:4480:PCE'MOA+203:8422.28'MOA+131:-7308.34'MOA+66:8422.28'
PRI+AAB:351.13::GRP:100:PCE'PRI+AAA:187.9973:::100:PCE'RFF+YC1:0000006325'ALC+A+++1+DI'PCD+3:45.82'
MOA+8:7207.77'ALC+A++++E01'PCD+3:1.18'MOA+8:100.57'MOA+25:8522.85'UNS+S'CNT+2:1'MOA+77:9011.84'
MOA+79:8422.28'MOA+125:8422.28'MOA+124:589.56'TAX+7+VAT+++:::7'MOA+79:8422.28'MOA+124:589.56'
MOA+125:8422.28'UNT+67+77'UNZ+77+V434687'</Content></ns:Mail>


 

HTML Email :

 



NOTE: This approach only works with non binary attachments.
3 Comments
Labels in this area