You are probably familiar with sending email using PL/SQL; there's the UTL_SMTP package, the APEX_MAIL package, and various other utilities that do this.
But what about reading email using PL/SQL? You need to investigate the POP and IMAP protocols. A quick search on Google turns up some sample code and even a feature-rich package, although the latter requires Java in the database (which I try to avoid whenever possible, and which is not available in Oracle XE in any case).
Approaching this from a different angle, a lot of companies use Microsoft Exchange as their mail server. Exchange has (since at least Exchange 2007) a robust set of web services that expose its features:
Exchange Web Services (EWS) provides the functionality to enable client applications to communicate with the Exchange server. EWS provides access to much of the same data that is made available through Microsoft Office Outlook. EWS clients can integrate Outlook data into Line-of-Business (LOB) applications. SOAP provides the messaging framework for messages sent between the client application and the Exchange server. The SOAP messages are sent by HTTP.
This figure from Microsoft shows the steps involved in calling the web service:
So it's basically SOAP (XML) over HTTP. We can easily do that in PL/SQL!
Use CasesOf course, you can use Exchange Web Services for lots of things besides just reading your email, for example:
- Integration with other systems via email messages (your PL/SQL program checks email periodically and processes the contents of the email and/or attachments)
- Searching email archives, combining the search results with search results from the database
- Checking availability of people for scheduling
- Adding or updating appointments and tasks
- Sending emails
- And lots more
You are welcome to add a comment below if you have more good examples of how the Oracle Database and Microsoft Exchange can work together.
PrerequisitesThe building blocks we need to work with Exchange Web Services in PL/SQL are:
- For building and parsing the SOAP messages we can use the built-in XMLTYPE data type.
- For calling the web service we can use the APEX_WEB_SERVICE package, or alternatively the FLEX_WS_API package (which is more or less the same package as that included in Apex, but the code is not wrapped, so you can study and modify it).
- Your Microsoft Exchange server is very likely set up with Integrated Windows Authentication, which means we need to authenticate to the web service using NTLM. I wrote a package last year called NTLM_UTIL_PKG which allows us to do just that.
- Your Microsoft Exchange server is probably also set up with SSL security, in which case you need to set up an Oracle Wallet with the SSL certificate of the Exchange server. There is a good explanation of that here and here. (By the way, there has been some confusion as to whether you can use Oracle Wallets with Oracle XE, as the Wallet Manager is a feature of the Advanced Security Option not included in XE, but this old thread states that "only the latter [call ins (i.e client to database network encryption)] requires ASO, SSL call outs don't". Since we are doing callouts from the database to Exchange that should be okay then.)
The MS_EWS_UTIL_PKG package
Given the above building blocks, I have started implementing a PL/SQL package that offers an easy-to-use API for working with Exchange Web Services (EWS). The name, unsurprisingly, is MS_EWS_UTIL_PKG.
Currently, the quite-not-yet-ready-for-public-consumption package handles the following operations:
- Get Folder Id By Name
- Find Folders
- Find Items
- Get Item
- Get File Attachment
- Move Item
- Delete Item
Note that an "Item" in Exchange can be an email item, a calendar entry, a task, and so on. Currently, the package handles email items and calendar entries.
So here's me, checking my inbox from SQL*Plus....
Of course, if you wanted an actual inbox in your database, you could just run "create or replace view my_inbox_v as select * from table (ms_ews_util_pkg.find_items(...) )".
Or create an Interactive Report on top of it in Apex. Or create a Region Plugin for Apex that displays a mail folder. Or create an Apex calendar on top of the calendar entries.
The possibilities are many!
- Exchange Web Services Reference
- Simple example in VBScript, how to list contents of your inbox using EWS
Next StepsWhen I get this polished enough for an initial public release, I'm going to include it in the Alexandria Utility Library for PL/SQL.
Leave a comment below if you are interested in this package, and if there are any specific parts of the EWS API that you would like to see implemented (and why).