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 = “

” + CRLF
+”” + CRLF
+”” + CRLF
+”

” + CRLF
+”” + CRLF
+”

” + CRLF
+”

”+”TEST EMAIL”+”

”+ CRLF
+”

” + CRLF

+”DATE: ” +Date+ CRLF                                               //Can be populated dynamically
+”
Message ID: ” +msgID+ CRLF

+”

Please see the attachment for original File.” + CRLF

+”

” + CRLF
+”” + CRLF
+”” + CRLF;

//create XML structure of mail package

String output = “”
+ “”
+ “” + Mail Subject + “”
+ “” + sender email ID + “”
+ “” + Receiver email ID + “”
+ “multipart/mixed; boundary=”” + boundary + “””
+ “”;

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(“”.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 :


Sample Email with attachment
[email protected]@xyz.com
multipart/mixed; boundary=”–AaZz”
—-AaZz
Content-Type: text/html; charset=UTF-8
Content-Disposition: inline

 

TEST EMAIL

 

DATE: 20171129T231100Z

Message ID: f82b3c5c-cb1a-11e7-8fbe-0000182374d2

Please see the attachment for original File.

—-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′

HTML Email :

Email 2489026

NOTE: This approach only works with non binary attachments.

New NetWeaver Information at SAP.com

Very Helpfull

 

 

User Rating: Be the first one !