cancel
Showing results for 
Search instead for 
Did you mean: 

html content and .txt attachment in the email

Former Member
0 Kudos

Hi All,

i have a reqmt to send a html content and .txt attachment after reading a file from a ftp location

I was looking at the below link to achieve this using JAVA mapping but not sure how to send html content. let me know if anyone faced the same

Also, if you could detail out the coding here, that helps.

thx

mike

Accepted Solutions (1)

Accepted Solutions (1)

RaghuVamseedhar
Active Contributor
0 Kudos

Sample code. HTML can be generated using XSLT OR Java mapping. Going with Java mapping, to make input payload as attachment.


package javaapplication1;

import java.io.InputStream;

import java.io.OutputStream;

import com.sap.aii.mapping.api.AbstractTransformation;

import com.sap.aii.mapping.api.StreamTransformationException;

import com.sap.aii.mapping.api.TransformationInput;

import com.sap.aii.mapping.api.TransformationOutput;

public class JavaApplication1 extends AbstractTransformation {

    public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)

            throws StreamTransformationException {

        try {

            InputStream inputstream = transformationInput.getInputPayload().getInputStream();

            OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();

            // a) Just copy Input file content to Output attachment content

            byte[] b = new byte[inputstream.available()];

            inputstream.read(b);

            transformationOutput.getOutputAttachments().create("FileAttachment", b);

            // b) Populate XML for XIPAYLOAD

            // Can hard code html or use Parameterized Java Mappings or get values from byte[] b (above).

            String htmlContent = "<!DOCTYPE html>\n"

                    + "<html>\n"

                    + "<body>\n"

                    + "\n"

                    + "<table style=\"width:100%\">\n"

                    + "  <tr>\n"

                    + "    <td>Jill</td>\n"

                    + "    <td>Smith</td>\n"

                    + "    <td>50</td>\n"

                    + "  </tr>\n"                  

                    + "</table>\n"

                    + "\n"

                    + "</body>\n"

                    + "</html>";

            String mailOutput = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"

                    + "<ns:Mail xmlns:ns=\"http://sap.com/xi/XI/Mail/30\">\n"

                    + " <Subject>Subject</Subject>\n"

                    + " <From>namea@company.com</From>\n" // Can hard code or use Parameterized Java Mappings

                    + " <To>nameb@company.com</To>\n"     // Can hard code or use Parameterized Java Mappings

                    + " <Reply_To/>\n"

                    + " <Content_Type>text/html</Content_Type>\n"

                    + " <Content><![CDATA[" + htmlContent + "]]></Content>\n"

                    + "</ns:Mail>";

            outputstream.write(mailOutput.getBytes());

        } catch (Exception exception) {

            getTrace().addDebugMessage(exception.getMessage());

            throw new StreamTransformationException(exception.toString());

        }

    }

}

Former Member
0 Kudos

Hi Raghu

Thanks much. Very helpful. One question. How do I attach as .txt ? I don't see that in the code.

mike

RaghuVamseedhar
Active Contributor
0 Kudos

transformationOutput.getOutputHeader().setContentType("Filename.txt");

use this line of code before outputstream.write(mailOutput.getBytes()); 

Former Member
0 Kudos

thanks much

Former Member
0 Kudos

thanks much

Answers (1)

Answers (1)

Former Member
0 Kudos

Requirement is taking the source file from ftp location and attaching to the email and also extract some of the fields from the file and put it as html content (probably with XSLT)

thx

mike

Former Member
0 Kudos

Anyone who has faced this case before?