on 10-27-2015 11:12 AM
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("&", "&");
sbuf = sbuf.replaceAll("\"", """);
sbuf = sbuf.replaceAll("'", "'");
sbuf = sbuf.replaceAll("<", "<");
sbuf = sbuf.replaceAll(">", ">");
out.write(sbuf.getBytes("UTF-8"));
}
Please help
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
23 | |
11 | |
9 | |
8 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.