Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
ALERT: The support homepage carousel is not displaying. We are working toward a resolution.

Using cMail to send email with an attachment from a Route

100% helpful (1/1)
cancel
Showing results for 
Search instead for 
Did you mean: 
TalendSolutionExpert
Contributor II
Contributor II

Using cMail to send email with an attachment from a Route

Last Update:

Jan 22, 2024 9:35:30 PM

Updated By:

Jamie_Gregory

Created date:

Apr 1, 2021 6:17:48 AM

Problem Description

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.

0693p000008uM0AAAU.png

 

The cMail component is configured to send the email for the Route as follows:

0693p000008uMBvAAM.png

 

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) ~[?:?]

 

Root Cause

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.

 

Solution

The avoid the exception perform the following:

  1. Attach the attachment as type, application/octet-stream, as shown in the following Job:

    0693p000008uLxfAAE.png

     

  2. 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.

Labels (1)
Version history
Last update:
‎2024-01-22 09:35 PM
Updated by: