Send an Email with a binary attachment from Oracle Service Bus – Part 4

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>
    <complexType name="EmailType">
        <sequence>
            <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" />
        </sequence>
    </complexType>

    <complexType name="AttachmentType">
        <sequence>
            <element name="name" type="string" minOccurs="1" />
            <element name="type" type="string" minOccurs="1" />
            <element name="content" type="base64Binary" minOccurs="1" />
        </sequence>
    </complexType>
</schema>

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:

SendEmailWithBinaryAttachmentProxyMessageFlow

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):

java-callout-properties

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)
            throws IOException
    {
        BASE64Decoder decoder = new BASE64Decoder();
        return decoder.decodeBuffer(base64binary);
    }

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.

insert-properties

The Transport Header action properties simply specify the values of the to and subject fields:

transportHeaderProperties

And finally, the Replace action simply sets the body of the email to be as defined in the input message:

replace-properties

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:Email xmlns:ema="http://www.oracle.com/Email">
    <ema:to>joe.bloggs@oracle.com</ema:to>
    <ema:subject>Email from OSB with binary attachment</ema:subject>
    <ema:body>This is the email body</ema:body>
    <ema:attachment>
        <ema:name>HelloWorld.pdf</ema:name>
        <ema:type>application/pdf</ema:type>
        <ema:content>JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURl

………(extra characters removed for brevity)……….

          CjE0MzQ5CiUlRU9GCg==</ema:content>
    </ema:attachment>
</ema:Email>

This should send an email to joe.bloggs@oracle.com, 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 :)

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

UK Pre-Sales consultant specialising in business integration.

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today