Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
You want to use a cMail component to send an email with an attachment using a Route. In this example, you use a cProcessor component to add the exchange body as an attachment.
The cMail component is configured to send the email for the Route as follows:
The cProcessor component attaches the exchange body as an attachment and sends the email as expected in Studio.
However, when the Route is deployed on runtime, the Job fails with the following exception:
Caused by: javax.activation.UnsupportedDataTypeException: plain/text at javax.activation.DataHandler.writeTo(DataHandler.java:75) ~[?:?] at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1692) ~[456:com.sun.mail.javax.mail:1.6.1] at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:996) ~[456:com.sun.mail.javax.mail:1.6.1] at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:561) ~[456:com.sun.mail.javax.mail:1.6.1] at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:84) ~[456:com.sun.mail.javax.mail:1.6.1] at javax.activation.DataHandler.writeTo(DataHandler.java:77) ~[?:?] at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1692) ~[?:?] at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1913) ~[?:?] at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1315) ~[?:?]
This issue is similar to the ones described below:
<https://stackoverflow.com/questions/49941300/javamail-not-working-in-osgiservicemix-karaf> apache camel - JavaMail not working in OSGI(ServiceMix/Karaf) - Stack Overflow<https://stackoverflow.com/questions/49941300/javamail-not-working-in-osgiservicemix-karaf> I created a Karaf Instance on a Service Mix(7.0.1) and deployed my bundles into it. The camel route is starting up properly, but always fails when it should send an email. With the following exce... https://issues.jboss.org/browse/ESB-529?_sscc=t [ESB-529] A camel route contains camel-mail endpoint does not work if deployed into Fuse ESB 4 container - JBoss Issue Tracker<https://issues.jboss.org/browse/ESB-529?_sscc=t> It works fine if I have this route in a standalone Camel application. However, if I build it as a bundle and deploy it into Fuse ESB 4.0.0.3 container, it would not work.
The avoid the exception perform the following:
Attach the attachment as type, application/octet-stream, as shown in the following Job:
Configure the cProcessor component to attach a file to the email that goes out, using the following code:
import javax.mail.util.ByteArrayDataSource;
import org.apache.camel.Exchange; byte[] file = exchange.getIn().getBody(byte[].class); String contentType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class); if (contentType == null || contentType.isEmpty()) contentType = "application/octet-stream"; String fileId = exchange.getIn().getHeader("CamelFileName",String.class); exchange.getIn().addAttachment(fileId, new javax.activation.DataHandler(new ByteArrayDataSource(file, contentType))); exchange.getIn().setBody(fileId + " : processed in ESB")
The sample Job is attached to this article in the Route_With_Mail_Attachment.zip file.