cancel
Showing results for 
Search instead for 
Did you mean: 

java.io.ioexception server not responding ok to MAIL FROM;501 5.1.7 Bad Server error syntax

0 Kudos

Hi Experts,

Kindly help me with this error in receiver mail adapter. I  tried using Java mapping to send a file as attachment in mail .

Jre version :6

Jse Version :1.6

PI : 7.40

Code where my E-mail Id is specified :

public void execute(InputStream in, OutputStream out) throws StreamTransformationException {

  

  

     String mailSubject = "hi";

     String mailSender = "\"<abc@xyz.com>";

     String mailReceiver = "\"<abc@xyz.com>";

     String attachmentName = "mail.txt";

     String replyto="";

     String boundary = "--AaZz";

     String mailContent = "test mail";

     String CRLF = "\r\n";

     try {

       // 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>" + mailSubject + "</Subject>"

         + "<From>" + mailSender + "</From>"

         + "<To>" + mailReceiver + "</To>"

         + "<Reply_To>" + replyto + "</Reply_To>"

         + "<Content_Type>multipart/mixed; boundary=\""

         + boundary

         + "\"</Content_Type>"

         + "<Content>";

         out.write(output.getBytes());

  

       // create the declaration of the MIME parts

       //First part

       output = "--" + boundary + CRLF

                 + "Content-Type: text/plain; charset=UTF-8" + CRLF

                 + "Content-Disposition: inline" + CRLF + CRLF

                 + mailContent + CRLF + CRLF

  

  

       //Second part

                 + "--" + boundary + CRLF

                 + "Content-Type: application/xml; name=" + attachmentName + CRLF

                 + "Content-Disposition: attachment; filename=" + attachmentName + CRLF + CRLF;

       out.write(output.getBytes());

       //Source is taken as attachment

       copySource(in, out);

       // last boundary

       output = CRLF + CRLF +"--" + boundary + "--" + CRLF;

       out.write(output.getBytes());

       // finish mail package

       out.write("</Content></ns:Mail>".getBytes());

     } catch (IOException e) {

       throw new StreamTransformationException(e.getMessage());

     }

   }

  

  

  

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

  

  

  

     byte[] buf = new byte[in.available()];

     in.read(buf);

     String sbuf = new String(buf);

     // replace all control characters with escape sequences

     sbuf = sbuf.replaceAll("&", "&amp;");

     sbuf = sbuf.replaceAll("\"", "&quot;");

     sbuf = sbuf.replaceAll("'", "&apos;");

     sbuf = sbuf.replaceAll("<", "&lt;");

     sbuf = sbuf.replaceAll(">", "&gt;");

     out.write(sbuf.getBytes("UTF-8"));

  

  

   }

Please help

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

Hi Eng swee ,

I have taken the code from the link you suggested , but there was no main function . Hence i added it but not sure if its the right place

Code :

package sample;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import javax.xml.bind.DatatypeConverter;

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 Myattachment extends AbstractTransformation {

   String attachmentName = "mail.txt";

   String boundary = "--AaZz";

   String mailContent = "test mail";

   String CRLF = "\r\n";

   public static void main(String args[])

  {

  }

   public void transform(TransformationInput arg0, TransformationOutput arg1)  throws StreamTransformationException {

 

     InputStream in = arg0.getInputPayload().getInputStream();

     OutputStream out = arg1.getOutputPayload().getOutputStream();

 

   

     try {

       // create the declaration of the MIME parts

       //First part

       String output = "--" + boundary + CRLF

         + "Content-Type: text/plain; charset=UTF-8" + CRLF

         + "Content-Disposition: inline" + CRLF + CRLF

         + mailContent // this should be some more useful text

         + CRLF + CRLF

 

       //Second part

         + "--" + boundary + CRLF

         + "Content-Transfer-Encoding: base64" + CRLF

         + "Content-Type: application/pdf; name=" + attachmentName + CRLF

         + "Content-Disposition: attachment; filename=" + attachmentName + CRLF + CRLF;

       out.write(output.getBytes());

 

       // convert InputStream to Byte array

       byte[] input = new byte[in.available()];

       in.read(input);

 

       // convert payload to base64

       output = DatatypeConverter.printBase64Binary(input);

 

       // split lines after 76 rows

       output = addLinefeeds(output);

       out.write(output.getBytes());

 

       // last boundary

       output = CRLF + CRLF +"--" + boundary + "--" + CRLF;

       out.write(output.getBytes());

     } catch (IOException e) {

       throw new StreamTransformationException(e.getMessage());

     }

   }

   

   public String addLinefeeds(String str) {

 

     StringBuffer result = new StringBuffer(str);

     int n = 76; // split by 76 characters (maximum of numbers in lines)

     int l = str.length();

     int i = n;

     while (l > n) {

       result.insert(i, CRLF);

       i = i + n + 2;

       l = l - n;

     }

     return result.toString();

   }

  }

I got the error as attached. I have also attached my SXMB_MONI screen shot and output of java program.

Please help.

Thank you

engswee
Active Contributor
0 Kudos

You need to write the Java mapping in an external Java IDE and compile it as a JAR and import it - search on Java mapping in SCN to find out how it's done.

Alternatively, you can

0 Kudos

I tried to execute the java file by placing it my desktop. The JDK version was 1.6 and i also placed the external JAR file in the bin folder of my JDK.

I executed my java program , by setting the JDK path in CMD.

I got error for all the declarations related to import com.sap.aii.mapping.api.*;

Please help.

Thank You.

0 Kudos

I had got the output for JAVA code as attached in the screen shot , but this does not match the Output that was given in the blog.The content was not dislpayed and hence the error i got in the PI communication channel exists.

0 Kudos

Hi Eng ,

I got the required scenario executed. I had done a simple mistake in the receiver  communication channel .

I had used the mail package with content encoding as BASE 64. when i changed it to NONE , the Scenario worked

Thank you so much for your help

engswee
Active Contributor
0 Kudos

Glad to hear that your issue has been resolved. Glad to have been of assistance

Answers (1)

Answers (1)

engswee
Active Contributor
0 Kudos

Hi Sukanya

First of all, I'd suggest you go for Stefan's latest blog on this which do not use Mail Package since Mail Package is deprecated.

After that, try testing again. If you still get any error, I'd suggest you provide the output of the Java mapping as well as a screenshot of the error.

Rgds

Eng Swee