Sending an Email with an attachment from Oracle Service Bus – Part 3

Some time ago I wrote a two-part blog post describing how you could use Oracle Service Bus to send email and thought I’d covered just about everything people wanted to know about email. However, over the last few weeks several people have asked how to send email with attachments from Oracle Service Bus, something I didn’t cover in those blog posts, and so this is effectively Part 3.

If you didn’t read my earlier blog posts on sending email (Part 1 and Part 2), I suggest you read them now, as I will be referring to them in the remainder of this post.

Developing a service to send an email with an attachment from Oracle Service Bus is not much different from what I have described so far. In general, you will have an email business service which is responsible for actually sending the email (Part 1), and a proxy service, which routes to this email business service, responsible for defining the actual content of the email and attachment(s) (Part 2). The basic structure of the proxy service message flow for sending an email with an attachment will look something like:

emailWithAttachment

We have a single Route node containing a Routing action configured to send messages to our email business service. As in Part 2, the Request Action flow of the Routing action contains the same Replace action (to set the body of the email message) and a Transport Header action (to set the subject of the email message). The key difference here is the addition of the Insert action which describes the attachment we are going to send:

insertProperties2

As you can see in the image above, we are setting the first child element of the attachments context variable to be the XML expression in the yellow box. This expression is the key to sending an attachment, so lets look at it in more detail:

<con:attachment xmlns:con=”http://www.bea.com/wli/sb/context”>….</con:attachment>

This is the wrapper element required for any attachment.

<con:Content-Type>text/plain</con:Content-Type>

This element defines the Internet media type (aka MIME type) of the attachment – in this case a simple text file attachment.

<con:Content-Disposition>attachment; filename=”Simple.txt”</con:Content-Disposition>

This element specifies how the attachment should be handled by the recipient – in this case as an attachment (as opposed to being inline) with a filename of Simple.txt.

<con:body>I am the attachment contents</con:body>

This element represents the actual contents of the attachment.

But I don’t want the filename and/or contents to be hardcoded?

Then simply replace the static values with XPath expressions which will evaluate to the filename and/or contents of your attachment, for example:

<con:attachment xmlns:con="http://www.bea.com/wli/sb/context">
  <con:Content-Type>text/plain</con:Content-Type>
  <con:Content-Disposition>attachment; filename="{$body/email/attachment/filename/text()}"</con:Content-Disposition>
  <con:body>{$body/email/attachment/contents/text()}</con:body>
</con:attachment>

Note the use of the curly braces to ensure this is evaluated as an XPath expression and not just text.

What if my attachment isn’t text?

If your attachment is a binary attachment, such as a PDF, Microsoft Office Document or image, your Insert action properties will be exactly the same, apart from the XML expression which will be slightly different. For example, lets imagine we want to send an attachment called Simple.pdf, our expression will look something like:

<con:attachment xmlns:con="http://www.bea.com/wli/sb/context">
  <con:Content-Type>application/pdf</con:Content-Type>
  <con:Content-Transfer-Encoding>base64</con:Content-Transfer-Encoding>
  <con:Content-Disposition>attachment; filename="Simple.pdf"</con:Content-Disposition>
  <con:body>{$body/ctx:binary-content}</con:body>
</con:attachment>

Note the differences from our earlier example:

- The Internet media type (the Content-Type element) is now the one for a PDF file: application/pdf (for other types of attachment refer to the list of Internet media types here to see what this value should be set to)

- We have added a Content-Transfer-Encoding element which describes how the attachment is encoded (normally in Oracle Service Bus this will be base64).

- The body element (remember this is of the attachment) refers to the binary content within the body of the original message.

But how would I get binary content in the body of the message in the first place?

The simplest way is to make sure your proxy service is a messaging service, using the file transport, expecting an input message of type binary (e.g. a binary file placed in a folder). Oracle Service Bus would read in this file and create a reference to the binary file contained within the ctx:binary-content element within the body. Alternatively, your proxy service may be reading in email which contains binary attachments already.

You can read more about how binary content is handled in Oracle Service Bus here in the documentation.

How can I send multiple attachments?

Simply add another Insert action to insert another attachment to the attachments variable. Unless you are worried about the order of the attachments (only likely for inline attachments) then the only piece that will be different is the content of the XML expression.

   

So, in summary I have shown how to:

- Build a proxy service to send an email with an attachment from Oracle Service Bus

- Configure the Insert action to:

- Send a text or binary attachment

- Dynamically set the filename and contents of the attachments

- Send an email with multiple attachments

If you have anymore questions about how to use the email capabilities of Oracle Service Bus, then let me know by commenting.

Comments:

Thanks a lot Chris for this blog. It has been of great help. -Pooja

Posted by guest on April 02, 2009 at 11:51 AM BST #

Hi Chris, Thanks a lot for that excellent article. it cleared a lot of doubts.I still have a problem that is when i put a base 64 converted form of a pdf file in the body and then send it,it sends a pdf document as an attachment but it fails to open it at the receiving end? Has it got to do with the way i am sending it directly or should i use another way?

Posted by Priyank on April 02, 2009 at 01:13 PM BST #

Hi Priyank, Can you send me a sample OSB config file demonstrating your problem so I can take a look? Thanks, Chris

Posted by Chris Tomkins on April 03, 2009 at 03:24 AM BST #

Hi Chris, I tried sending a ms-word/pdf/jpg attachment by putting it's corresponding base64 in the message body. However, it shows error while opening the attachmen the mail received, saying that the content in the file is corrputed. Any idea what am I doing wrong? I guess, Oracle Service bus is unable to decode the base64 binary in the required file format. -Pooja

Posted by Pooja on April 03, 2009 at 04:34 AM BST #

Hi Chris, Should I send u the jar file for the project in which the proxy and other settings are kept? Please tell me how do i send it to you.For that reason i can tell yoy my problem I am using a wsdl to set my transport headers besides using the element in the same wsdl to set my binary content for the email attachment. If i assign base 64 content to this element and send it, an email is sent with an attachment but the attached pdf is corrupted. Waiting for your reply. -Priyank

Posted by Priyank Srivastava on April 03, 2009 at 04:47 AM BST #

Hi Chris...Great article !! I am doing a similar thing in my proxy service.My problem is i need to decode the base64 attachment ot xml before routing.Is there a Xquery and XSLT to to do the same. Thanks in advance, Neha

Posted by Neha Rathi on May 18, 2009 at 12:24 AM BST #

Hi Chris...Great Article !! I am doing something similar.Is there a XQuery or XSLT to decode base64 to XML in OSB ? Thanks in advance, Neha

Posted by Neha on May 18, 2009 at 12:26 AM BST #

Hi Chris, I need to send in Email Body Content (which is just a few lines of text) along with an Attachment. So I created a Proxy1 with SOAP/HTTP with WSDL, where I send in a Request (consisting of email address, subject, body content) along with the SOAP attachment. This proxy then routes it to a Local Proxy2, which is exactly as you have created in Part 2 which then routes it to Email Business Service as you have in Part 1. When I send in the SOAP Attachment along with the request from the Proxy 1, the destination does receive the email along with the attachment but for some reasons, the attachment is corrupted. Is there any way you can help me out so that I can send in Email Body Content (which is just a few lines of text) along with an Attachment which is not corrupted at destination? Thanks and Regards, Ash

Posted by Ash on June 24, 2009 at 11:07 AM BST #

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