Question
1
Replies
212
Views
Posted: December 25, 2019
Last activity: December 25, 2019
Attaching work object attachments as embedded file within .eml file that is created for outbound email.
The requirement is to create .eml file for the outbound email with attachments and attach the .eml file wth attachments in Wo.
Able to create the .eml file for the email without attachments but not able to create with attachments embedded within the .eml file.Written custom java codes to generate the .eml file but when with attachments it is not generating the .eml file.Need help
Below is the java code that is written that is not working to embed the attachments in .eml file. The attachment are mainly .pdf files:-
PublicAPI tools = ThreadContainer.get().getPublicAPI();
try {
javax.mail.Message message = new javax.mail.internet.MimeMessage(javax.mail.Session.getInstance(System.getProperties()));
ClipboardPage mypage = tools.findPage("WorkAttachments");
ClipboardProperty WrkAttList = mypage.getProperty("pxResults");
java.util.Iterator Attlist = WrkAttList.iterator();
ClipboardPage thisAttachment = null;
message.setFrom(new javax.mail.internet.InternetAddress(From));
message.setRecipients(javax.mail.Message.RecipientType.TO,
javax.mail.internet.InternetAddress.parse(To));
message.setSubject(Subject);
// create the message part
javax.mail.internet.MimeBodyPart content = new javax.mail.internet.MimeBodyPart();
// fill message; body is String type
//content.setText(Body);
content.setContent(Body,"text/html");
message.setContent(Body, "text/html");
javax.mail.Multipart multipart = new javax.mail.internet.MimeMultipart();
multipart.addBodyPart(content);
while (Attlist.hasNext())
{
//messages= "am in while block";
thisAttachment = ((ClipboardProperty) Attlist.next()).getPageValue();
byte[] docContentByteArray = Base64Util.decodeToByteArray(thisAttachment.getString("pyAttachStream"));
fileName = thisAttachment.getString("pxAttachName");
HashStringMap aMap = new HashStringMap();
aMap.put("ContentDisposition","inline");
//Converting the bytestream to physical file and store it in service directory
String result=tools.sendFile(docContentByteArray,fileName,true,aMap,false);
javax.mail.internet.MimeBodyPart attachments = new javax.mail.internet.MimeBodyPart();
String filepath ="file://web:/StaticContent/global/ServiceExport/"+fileName;
// Taking the physical file and embedding in the .eml file.
javax.activation.DataSource source = new javax.activation.FileDataSource(filepath);
attachments.setDataHandler(new javax.activation.DataHandler(source));
attachments.setFileName(fileName);
multipart.addBodyPart(attachments);
}
message.setContent(multipart);
message.writeTo(
new java.io.FileOutputStream(new java.io.File("WOEmailAttachment.eml")));
System.out.println("End of try block");
//anopther java for
java.io.InputStream is = new java.io.FileInputStream("WOEmailAttachment.eml");
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(is.available());
org.apache.commons.codec.binary.Base64OutputStream baos2 = new org.apache.commons.codec.binary.Base64OutputStream(baos);
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = is.read(buffer, 0, 4096))!=-1) {
baos2.write(buffer, 0, bytesRead);
}
baos.flush();
baos.close();
is.close();
ClipboardPage attachPage = tools.findPage("pyNewFileAttachment");
ClipboardProperty attachStream = attachPage.getProperty("pyAttachStream");
attachStream.setValue(baos.toString());
//end
} catch (Exception e) {
System.out.println("In catch block");
System.out.println(e.getMessage());
}