Email with HTML content and attachment with help of Java Mapping
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”; mailContent = “<html>” + CRLF +”div#banner {” + CRLF +”.right {” + CRLF +”</style>” + CRLF +”<b>DATE: </b>” +Date+ CRLF //Can be populated dynamically +”<br/><br/>Please see the attachment for original File.” + CRLF +”</div>” + CRLF //create XML structure of mail package String output = “<?xml version=\”1.0\” encoding=\”UTF-8\”?>” out.getOutputPayload().getOutputStream().write(output.getBytes()); // create the declaration of the MIME parts //First part. It will contain the HTML content output = “–” + boundary + CRLF //Second part. It contains data to be sent as an attachment. + “–” + boundary + CRLF out.getOutputPayload().getOutputStream().write(output.getBytes()); } catch (Exception e){} } protected static void copySource(InputStream in, OutputStream out) throws IOException, StreamTransformationException int c; try{ } 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.
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
The above code gives a lot of exceptions/errors. Can you paste the completed and compiled code?
Hello Dave,
Could you please share the error details?