Skip to Content

In a previous blog post of mine, I discussed how you can replace the body of a Cloud Integration message with the contents of an attachment. Tom van Rooijen commented on that post, asking how you can add a binary attachment to a Cloud Integration message, and have that attachment delivered by a receiver mail channel. Since the answer is probably of interest to others, I’m going to describe an approach to solving Tom’s problem in this blog post.

The addAttachmentObject method

Unsurprisingly, the key to adding an attachment to a message is the Message interface. I’ve previously blogged about the methods of this interface, and among them we find the addAttachmentObject method:

public void addAttachmentObject(java.lang.String, org.apache.camel.Attachment)

The method expects a string, which is the name of the attachment, and an object of a class implementing the Apache Camel Attachment interface. The Attachment interface is implemented by the DefaultAttachment class located in the org.apache.camel.impl package.

Creating an attachment object

In order to construct a DefaultAttachment object, we need an object wrapping the actual contents of the attachment. In the following, I will assume that we have the contents in a byte array. In Java we have a convenient way of wrapping a byte array: The ByteArrayDataSource class, which implements the DataSource interface. Luckily, this class is available in Cloud Integration. To construct a ByteArrayDataSource object, we need to provide the byte array and a string. The string is the MIME type of the attachment.

Configuring the receiver mail channel

In order for the receiver mail channel to pass our attachment on to the email’s recipient, we need to instruct it to add attachments from the message. To do so, go to the channel’s Connection tab, and make sure that the Add Message Attachments checkbox is selected.

Assembling the pieces

We now know everything we need to know in order to add an attachment in code. In the Groovy code below, the attachment is a PNG image, which I decode from Base64 in order to get a byte array. The binary content can be anything else, of course (but remember to adjust the MIME type accordingly).

import org.apache.camel.impl.DefaultAttachment
import javax.mail.util.ByteArrayDataSource

def Message processData(Message message) {

   // 1: Get the binary attachment content in the form of a byte array
   def imageBytes = 'iVBORw0KGgoAAAANSUhEUgAAAFsAAAALCAIAAACf2mY5AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAEySURBVEhL7ZTBGcMgCIUzlwM5j9NkmQyTPggq+IGml6SH/idFwOcL7Xb+sZAje96YvOtlgJuxLnucuSQ+VUdHSVsqBy1lRkpqx3tORZYxKr/jBt9lIskewSLZTh2pPm/Vv8YtR3r518ODTwauW+n78YYOvJ58nkpxpmKQJK04aemIZBLX3VRda7C2g7N2RJfTfaOnC1CumtFXQnnck41aSeKKtrT5CMg2nhEuUpgnrR2RRwj2gXdgGVV4ySIp6mlPOvraQcOgqG1jR9DAzoVmaHdhgvb+SPGEPQNUoTRJq7jnHUeQ1GaE1t5MAXKkTgNlyM+HkyGANxdOSAbHDfa2jBUWqdFQV36A9sHpOdzuBqskFYbP9f8BKE0yIw9Cl6/9eI/HHYEh8Y/xF3h+Rn6b8/wA8YzyCnrn53gAAAAASUVORK5CYII='.decodeBase64()

   // 2: Construct a ByteArrayDataSource object with the byte array and the content's MIME type
   def dataSource = new ByteArrayDataSource(imageBytes, 'image/png')

   // 3: Construct a DefaultAttachment object
   def attachment = new DefaultAttachment(dataSource)

   // 4: Add the attachment to the message
   message.addAttachmentObject('hello-world.png', attachment)
   return message


Execute the code in a script step in your integration flow, and the following image will be attached to the message and delivered by the receiver mail channel:

To report this post you need to login first.


You must be Logged on to comment or reply to a post.

  1. Tom van Rooijen

    Hello Morten,


    Thank you for this write up, I have tested it and it works of course.
    I understand the attachment checkbox in the mail adapter better now.

    The remaining item is to get the pdf that I have created (I think) in groovy into to a base64 format so that the decodeBase64 can do its work.
    The pdf is generated from an example like this:

    import java.util.HashMap
    import org.apache.camel.impl.DefaultAttachment
    import javax.mail.util.ByteArrayDataSource
    import com.itextpdf.text.Document
    import com.itextpdf.text.Paragraph
    import com.itextpdf.text.pdf.PdfWriter
    def Message processData(Message message) {
           def body = message.getBody();
          // step 1
        def document = new Document()
        println("Document Created")
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream("pdfatt"))
        println("PdfWriter Created")
        // step 3
        println("Document Opened")
         // step 4
        document.add(new Paragraph("Hello Groovy!"))
        println("Content Added")
        // step 5
        println("Document Closed")

    Now how can I get the binary content of this “pdfatt” object so I can generate the message attachment?

    Thanks & regards


    1. Morten Wittrock Post author

      Hi Tom

      Base64 is not needed; that was just a convenient way for me to get some binary content into the script.

      What you need, is to get at the binary contents of the PDF generated by the iText library.

      The PDFWriter.getInstance method requires an OutputStream object. One way to solve this is to pass it a ByteArrayOutputStream, which collects bytes into a byte array. So something like the following:

      // Construct a ByteArrayOutputStream object
      def bytes = new ByteArrayOutputStream()
      // Your other iText setup goes here
      // Pass the ByteArrayOutputStream to the getInstance method
      PdfWriter.getInstance(document, bytes)
      // Creating the actual PDF goes here
      // After document.close():
      def pdfBinary = bytes.toByteArray()

      At this point, the pdfBinary byte array contains the PDF document, which you can go on to create the attachment with. Remember to import the class.




Leave a Reply