Sending an Email from Oracle Service Bus - Part 2

Note: This post was first published before Oracle merged with BEA when the Oracle Service Bus product was known as AquaLogic Service Bus, hence the occasional reference to BEA and AquaLogic Service Bus.

In my previous post I showed how easy it was to create a business service in Oracle Service Bus which could be used to send email. If you completed the steps and created this service in your own Oracle Service Bus environment you may have noticed a few limitations:

- It only ever sends email to one address

- The email sent doesn't have the subject field populated

As pointed out in a comment on my first post - one way of addressing the first limitation is to create a separate business service for each email address - not exactly a scalable approach if you have 3000 different email addresses to send email to! Thankfully there is a better way to address both limitations - the Transport Headers action which allows you to modify the transport specific headers of the outbound request and/or inbound response from within a proxy service. In our case, we are interested in setting the email transport specific headers in the outbound request.

In order to demonstrate this, lets make our existing email business service more useful by fronting it with a proxy service which sets the email subject, content and destination email address dynamically based on information in an incoming request of the form:

<email>

  <to>The destination email address</to>

  <subject>The subject of the email</subject>

  <content>The content of the email</content>

</email>

Lets start by creating a simple XML schema to represent our incoming request:

- In the service bus console, click Create in the Change Center to start a new session

- Navigate to the project you created your email business service in

- In the field Enter New Folder Name box type XML Schema and click Add Folder

- Click on the newly created XML Schema folder

- From the Create Resource drop down select XML Schema and complete the screen as follows:

emailServiceConfiguration2

Make sure you enter the XML schema which is:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="
http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/EmailRequest" xmlns:tns="http://www.example.org/EmailRequest" elementFormDefault="qualified">

    <element name="Email" type="tns:EmailType"></element>
    <complexType name="EmailType">
        <sequence>
            <element name="to" type="string"></element>
            <element name="subject" type="string"></element>
            <element name="content" type="string"></element>
        </sequence>
    </complexType>
</schema>

- Click Save

The next step is to create the proxy service which accepts an XML message conforming to the schema you have just created:

- Add a new folder to your project called Proxy Services and navigate to this folder

- From the Create Resource drop down choose to create a new Proxy Service

- Complete the first screen as follows:

emailServiceConfiguration1

- Click Next

- Select XML as the Request Message Type and select Browse

- Select the EmailRequest schema you have just created

- Select the Email element and click Submit

- Click Last as we do not need to modify any other settings for this service

- Check the summary screen looks like the one below:

emailServiceConfiguration3

Note: By clicking the Last button we have accepted the defaults and created an XML over HTTP service (although we could equally well have chosen to create a WSDL based service too).

- Click Save

The next step is to create the message flow for the proxy service:

- Click on the message flow icon in the Actions column and you should see a very simple message flow:

messageFlow1

- Click on the EmailService node and select Add Route

- Click on the route node created and choose Edit Name and Description. Give the route node a sensible name and description and click Save

- Click on the route node again and select Edit Route

- Click on Add an Action and select Communication>Routing

- In the Route to action just created, click on Service, select the email business service you created previously, and click Submit

- Back in the Route to action add a request action by clicking on Add an Action under Request actions and select Communication>Transport Headers

- Click on Add Header

- From the drop down select email>Subject

- Set the Set Header to Expression to $body/ema:Email/ema:subject/text() (you can use the Variable Structures section on the left hand side to select the correct structure)

- Click on the document icon to the left of the Subject radio button and select Add Header

- Repeat the Add Header steps to set the To field to $body/ema:Email/ema:to/text()

Next we have to replace the main body of the message with just the content part to ensure our email contains the content rather than the original XML request sent to the proxy service:

- Click on the icon to the left of Set Transport Headers and select Add an Action>Message Processing>Replace

- Ignore the XPath field (by default it selects everything which is what we want in this case)

- In the variable field enter body

- Set the Expression field to be $body/ema:Email/ema:content/text()

- Ensure the Replace node contents radio button is selected

- The completed Route to part of the flow should look like:

messageFlow2

- Click Save All

- Click Activate, enter a description and click Submit to enforce your changes.

The final step is to test our new proxy service. Click on the bug icon to launch the Test Console and modify the contents of the to, subject and content XML fields and click Execute. Check your email to make sure it has been sent and is formatted correctly.

In summary, in this post we have taken a simple email business service and fronted it with a proxy service. This has turned our limited email service into a much more flexible service which can easily be reused by other services. We have also showed how to expose a service using a different transport protocol (in this case we have exposed our email business service as an XML/HTTP proxy service) and ensured our service consumers are not tightly coupled to our email business service making it easier for us to make changes.

Comments:

Hi Chris, I just wanted to ask you if after configuring this proxy and business service you call them with a Service Call Out, or there is another way I just don't know. Thanks Andres Romero

Posted by Andres Romero on October 17, 2008 at 01:11 PM BST #

Hi Andres, Once you have developed the proxy service described there are a number of ways you can call it from a proxy service message flow: - Via a routing action - Via a publish action (since it is a one-way operation) - Via a service callout as you correctly point out If you do not wish to make this service publicly available then switch the proxy transport to be local, effectively making it a service that can only be called by other proxy services on the bus. Hope that helps, Chris

Posted by Chris Tomkins on October 19, 2008 at 03:46 AM BST #

Hi Chris, The blog helped me a lot to understand and work on alsb mailing part as there is so limited material available for it. It would be tremendous help if you could tell how to send attachments coming in the xml to the desired email ids.

Posted by Priyank on March 09, 2009 at 07:24 AM GMT #

Hi Chris, I wrote you earlier about sending email with attachments using oracle service bus Is there a way to do this any help is most welcome

Posted by Priyank on March 10, 2009 at 03:01 AM GMT #

Hi Chris, This blog has been a lot helpful. I have a situation where in my request XML payload contains attachment which I want to mail to a user. Do you know how can I send an email with this attachment? Thanks for all your efforts Chris! -Pooja

Posted by Pooja on April 02, 2009 at 03:30 AM BST #

Hi Priyank and Pooja, Check out my latest blog post: http://blogs.oracle.com/christomkins/2009/04/sending_an_email_with_an_attac.html which should help you get started with sending emails with attachments from Oracle Service Bus. Please comment on that post if you have any further questions. Thanks, Chris

Posted by Chris Tomkins on April 02, 2009 at 07:58 AM BST #

Hi Chris I know this is not directly related to your blog, but maybe you can help anyway. One of the e-mail transport headers is the "From", but it does not seem like setting the header overrides the definition given in the Business Service. Any experience on how to dynamically determine the "From"? Thanks Jens

Posted by jens on April 17, 2009 at 07:30 AM BST #

Hi Jens, Unfortunately it is not currently possible to set the From address dynamically, see the following note: http://download.oracle.com/docs/cd/E13159_01/osb/docs10gr3/userguide/modelingmessageflow.html#wp1120652 However, I can definitely conceive of scenarios where you would want to do this, e.g. a company sending out emails from different department email addresses wanting to share the same email business service and so I have raised this as a product enhancement request. The alternative approach is to create multiple email business services with different static from addresses. Although this means you have more services to maintain, these can share the same SMTP credentials and can be monitored individually. I hope this helps. Thanks, Chris

Posted by Chris Tomkins on April 22, 2009 at 04:14 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