Send an Email with a binary attachment from Oracle Service Bus – Part 4
By Chris Tomkins on Jul 16, 2009
When I started writing this sequence of articles on sending email from Oracle Service Bus I only expected to write 2 parts but following a number of requests asking how to send emails with attachments I wrote Part 3. Now, following a number of people having problems sending email with binary attachments, I’m writing Part 4 – hopefully this will be the last one!
In Part 3 I explained one method for sending a binary attachment, but this relied on your proxy service being a messaging service which had an input message type of binary – fine if the only variable in your outbound email is the binary attachment, but limited if you want a number of components (e.g. subject, to address, etc. ) to be variable.
So, lets create a proxy service of messaging type which has an input message type of XML defined by the following XML schema:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.oracle.com/Email" xmlns:tns="http://www.oracle.com/Email" elementFormDefault="qualified">
<element name="Email" type="tns:EmailType"></element>
<element name="to" type="string" minOccurs="0" />
<element name="subject" type="string" minOccurs="0" />
<element name="body" type="string" minOccurs="1" />
<element name="attachment" type="tns:AttachmentType" minOccurs="1" />
<element name="name" type="string" minOccurs="1" />
<element name="type" type="string" minOccurs="1" />
<element name="content" type="base64Binary" minOccurs="1" />
i.e. we can specify the to, subject and body fields of the email, plus the attachment name, type and content.
Hopefully you won’t be surprised to see the basic structure of the proxy service message flow is very similar to that described in Part 3:
Note the key differences are the addition of a Java callout (this decodes the incoming base64Binary content) and the Transport Header and Replace actions swapping places (this is only to ensure we only replace the payload of the message, once we have extracted all the pieces we are interested in).
The Java callout should look something like (note you may need to click on the images below to see the complete image):
This is basically invoking a Java method called decode, in the class BinaryConversion, in the package com.oracle, in the Jar file called binary.jar which will decode the incoming attachment content data. This is not built in functionality in Oracle Service Bus and so you will need to create this class file and Jar file yourself and add it in to your Oracle Service Bus project. The decode method should look like:
public static byte decode(String base64binary)
BASE64Decoder decoder = new BASE64Decoder();
The Insert action properties are basically the same as in Part 3, but with some of the hardcoded information replaced with XPath queries retrieving the information from the incoming message payload. Note the Content-Type must be a valid Internet Media type (MIME type), e.g. for a PDF file it should be application/pdf.
The Transport Header action properties simply specify the values of the to and subject fields:
And finally, the Replace action simply sets the body of the email to be as defined in the input message:
Note: In order to test this proxy service you will need a base64 encoded version of your attachment available. This can be obtained by using one of the many online tools such as the one available here which allows you to select your file and then convert it to base64 (simply copy and paste the text from the top window into the attachment content field in your test input data).
An example test might look something like:
<ema:subject>Email from OSB with binary attachment</ema:subject>
<ema:body>This is the email body</ema:body>
………(extra characters removed for brevity)……….
This should send an email to firstname.lastname@example.org, with the subject “Email from OSB with binary attachment”, with an email body “This is the email body” and a binary attachment HelloWorld.pdf containing whatever information it contained originally (in my case simply the text HelloWorld!).
Hopefully this helps answer all the questions on sending emails with attachments from Oracle Service Bus – at least for now :)