Skip to Content
Author's profile photo Ricardo Viana

Multiples content Email – File Attached and body text

Hi all,

     I will explain in this documento how to do a javamapping using DOM to create a MailPackage30 – xml of file attach with body text to e-mail.


     I will not explain about the difference between DOM or SAX and others stuffs.

     Check this out and enjoy it.

There are some documents below that explain, but its not clear check below:

XI Mail Adapter: An approach for sending emails with Attachement with help of Java mapping

XI Mail Adapter : Dynamically building attachment and message body content using a simple UDF

Whats the difference between java mapping SAP PI version 7.0 and 7.1 ?

Into SAP PI 7.0 you must implement the method execute and SAP PI 7.1 you must implements the transform method.

SAP PI 7.1 lib for java mapping: aii_map_api.jar

SAP PI 7.0 lib for java mapping :

Project Case:

My project case the client must provide for customer all XML files from NF-e for Brazil, so based on that, the java mapping and code of RFC it´s for this case, you just need adapt it for your project case.

Sem título.png

First step: Import the XiMail3.0.xsd:

Basically you must create a Product/SWCV into SLD system, after that import the SWCV into Repository, depends of version of SAP PI create a namespace and import the external definition.

The the snote below:

748024 – XI Mail Adapter XML Schema


Secund step: Check the Mail structure:

To create a e-mail with multiple content – (file,txt,xml,pdf,image,anything) and body text basically you must use the hilight elements:

My project was XML File with dinamic body text based on the XML file data.


Lets rock for java mapping (This code need be adapte for your case) code walk through:

My project was XML File with dinamic body text based on the XML file data.

Java code – Variables:

Some static variables

static String BSystem = "ECDCLNT130";  - Bussiness system of SAP ECC
static String CChannel = "RFC_MAIL_RCV"; - Communication Channel
static String emailFrom = ""; -- Field From
static String prefixoDocumento = "Attachment XML Name "; - Prefix of Document
static String prefixoSubject = "Attachment Xpto: " - Subject of email;

RFC Lookup:

// --------------------- RFC LOOKUP
Channel chn = null;
RfcAccessor rfc = null;
String email = "";
chn = LookupService.getChannel(BSystem,CChannel);
rfc = LookupService.getRfcAccessor(chn);
absTraceLog.addDebugMessage("Fazendo a chamada da RFC  ");
String req = "<ns0:NAME OF RFC xmlns:ns0='urn:sap-com:document:sap:rfc:functions'><CHAVE>" + idNFe + "</CHAVE></ns0:NAME OF RFC>";
absTraceLog.addDebugMessage("JAVAMAPPING --- Iniciando o RFC lookup  > "+ req);
InputStream inputRFC = null;
XmlPayload rfcPayload = LookupService.getXmlPayload(inputRFC);
XmlPayload result =;
InputStream resp = result.getContent();

As I mentioned before you must adapt this code for your project case !!!

Check below the RFC:

The field – CHAVE will be filed by –

XML Field below


//Java code to extract the value from tag chNFE using indexOf.
idNFe = inptxml.substring(inptxml.indexOf("<chNFe>") + 7, inptxml.indexOf("<chNFe>") + 51);

Basiclly the RFC receives the string from Access Key of XML File and abap code will runes tables ADR6,LFA1,KNA1 from ecc side to extract the e-mail from register.

Input data:


Export data:


The return from RFC the value will be capture by:

//Node to capture the value from the export structure from RFC - Field SMTP_ADDR
Node node = (Node) doc.getElementsByTagName("SMTP_ADDR").item(0);
if (node.hasChildNodes() && !node.getFirstChild().getNodeValue().equals("")) {
  email = node.getFirstChild().getNodeValue();

Using parsing via DOM to create the XIMail structure:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 DocumentBuilder db;
  try {
  db = dbf.newDocumentBuilder();
  TransformerFactory tf = TransformerFactory.newInstance();
  Transformer transform = tf.newTransformer();
  Document docout = db.newDocument();
  Element root = docout.createElement("ns1:Mail");
  root.setAttribute("xmlns:ns1", "");
  Element subject = docout.createElement("Subject");
  Text subjectText = docout.createTextNode(getPrefixoSubject()
  + idNFe);
  absTraceLog.addDebugMessage("Subject e-mail --- "+subjectText.toString());
  Element from = docout.createElement("From");
  Text fromText = docout.createTextNode(getEmailFrom());
  absTraceLog.addDebugMessage("From e-mail --- "+fromText.toString());
  Element to = docout.createElement("To"";
  Text toText = docout.createTextNode(email);
  Element contentType = docout.createElement("Content_Type");
  Text contentTypeText = docout.createTextNode("multipart/mixed;boundary=--A
  absTraceLog.addDebugMessage("Content_Type e-mail --- "+contentTypeText.toString());
  bodyText texto = new bodyTexti(nptxml);
  Element content = docout.createElement("Content");
  Text contentText=null;
  if("XPTO ".equalsIgnoreCase(getPrefixoSubject())){
  contentText = docout.createTextNode(texbodyTextText());
  DOMSource domS = new DOMSource(docout);
  transform.transform((domS), new StreamResult(out));

The main part to defined Mutiples content to Mail:

Text contentTypeText = docout.createTextNode("multipart/mixed;boundary=--AaZz");

The Mime – multipart/mixed;boundary – Allows to send multiple contents:

If you want learn more about mimes, check link below:

MIME – Wikipedia, the free encyclopedia

Creating the body text with XML as attachemet:

Body text example:

public String bodyText(String XML) {
    Text ="Hi, "+getGap()+getGap()+
    "Check attachment and do not asnwer this e-mail, it´s automatic message "+ getGap()+ getGap()+
    content = "----AaZz\r\n Content-Type: text/plain; charset=UTF-8\r\n" +
     "Content-Disposition: inline\r\n\r\n" + Text +
      "\r\n----AaZz\r\n Content-Disposition: attachment; filename="+ "AttachmentName " +
      ".xml" +"\r\n\r\n" + XML + "\r\n";
return content;

That´s it.

I hope that I helps you to understando about how to build XiMail3.0 structure and send e-mail with body text with Attachment of XML.



R Viana.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.