Skip to Content
avatar image
Former Member

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

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    avatar image
    Former Member
    Oct 28, 2015 at 08:51 AM

    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


    Capture3.PNG (27.4 kB)
    Capture4.PNG (20.4 kB)
    Capture1.PNG (159.5 kB)
    Capture2.png (85.9 kB)
    Add comment
    10|10000 characters needed characters exceeded

  • Oct 28, 2015 at 03:18 AM

    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.

    Create email with body and attachments for binary payload with Java mapping

    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

    Add comment
    10|10000 characters needed characters exceeded