Thursday 12 October 2017

OSB 12c : Email Polling with Attachment

This is what you should do if you want to use the email protocol to initiate your OSB service based on email in the Mail Inbox.

Overview:
The Proxy service polls the Mail Inbox for the new messages and invokes the Business service whenever there is a new message in the Inbox.
The polling interval can be specified in the Proxy Service configuration.

You can read the contents of the email or can process an email attachment.

We will be using the 12c JDeveloper to create the service. You can do the same on the service-bus console.

Steps:
1. Create an Service Bus Application and Project.



2. Name your application and project.





3. Once your application and project are created, create your folder structure like below:



4. First we will create a Service Account, with static username/password of the mail account.

 



5.  Save.
6. Now we will create a Business Service which will be invoked based on the email.

Note: This Business Service can do anything you want to do with the data you receive.
Example:
a. Invoke a service (say BPEL) with the email body payload as input or
b. Read the attachment and process to a file location using file transport

We will cover example b today.

7. Name the service and select file transport. We are not using the file adapter jca here.



8. Keep the Service Type below:

 

9. Define the server directory path starting with the root till the directory you want to place the file.



10. On the Business Service leave other values default, Save.
11. Open your composite.xml and on the External Services swim-lane  right click > Choose > Business Service > Select Business Service WriteEmail_BS > Ok






 You can also drag-drop your business service to the swim-lane.

12. On the middle swim-lane, Pipelines/Split Joins, right-click > Insert > Pipeline
13. Name your pipeline, change Location to Services\Proxy Service , Next


 
14. Define the Service Type as Messaging - Text/None and check Expose as a Proxy Service.
Name your Proxy Service with transport as email.



15. Finish.
16. Your composite should look like below:

 

17. Open the Proxy Service, click on the Transport tab and provide the mail server host and port



Here we are using Outlook Office 365 email server on port 993 or 143

18. Click on the Transport Details tab, select the service account we created, email protocol as imap, specify the polling interval as per requirement, select the Post Read Action as per the requirement and also specify the Download/Archive/Error directories accordingly and Save All.




19. Open you Pipeline, right-click and add a pipeline-pair node
20.  In the request pipeline stage add an Assign action. We will use it to get the attachment name.
This value is part of the context variable - $attachments
Use the below expression:



assign it to variable attachmentname.

 21. Add one Assign  and Replace actions to map the attachment contents to outgoing $body like below


 


22. Add one Publish action and set the target Service to the Business Service WriteEmail_BS.bix
23. Inside the Publish insert a Transport Header action, with below parameters




Test:

Send an email to the user account email.


This will trigger your service and as a result write the attachment to the file location on the server.


Helpful? Please Comment.

Happy Learning!!

8 comments:

  1. Isn't SSL a requirement when connecting to outlook.office365.com?

    ReplyDelete
    Replies
    1. When you are setting up your mail you have to add the certificates to the trust jks and then create the email driver

      Delete
  2. Its writing file but file is not readable.Writing N7FFB type file to target folder

    ReplyDelete
  3. Hi Nadim, what about writing multiple file attachments? I tried looping through the attachments, but the file contents of one file kept overwriting the contents of another.

    ReplyDelete
    Replies
    1. Hi,i have the same problem.Is there any solution?

      Delete
    2. Make sure your file name is not the same otherwise it will overwrite. You can add a dynamic value to the name in case you always receive the same file

      Delete
  4. Hi Nadim,
    If we have to read only.csv file from attached files in the email. What function we have to use. Like some times email attachment contains .jpeg images of signature company logos. Please help it is bit urgent

    ReplyDelete
  5. Hi Nadim,

    Is it possible to poll the inbound email based on a specific SUBJECT. That is if SUBJECT is matching then read the email, else do not poll

    Also Subject will keep changing each time

    Please advise.

    thanks

    ReplyDelete

OSB 12c : Dynamic Routing to Business/Proxy Service

Dynamic routing is a kind of  Content-based Routing pattern, which is used when the BusinessService/ProxyService path is required to be de...