APEX & Office 365 Email Integration

Search for a command to run...

excellent , all the best for author!
Wow this was really helpful. How do we get a list of all the attachments for an email message? We are only seeing the first one which is always the smime.p7m file Thank you
Never mind. I see now that the smime file contains the attachments.
Hi Jon, this post, and the previus ones, are amazing. I need to do exactly the same you explained here but I need to download the attachment files into the filesystem and not into the database. Can you give me some advise how to do it? Also I need to download only the unread emails from the mailbox. Which parameters do I need to set to avoid to read the same emails over and over again? Thank you very much.
Hi Jon, I like your service desk & invoicing scenarios - doubtless there are many others along the same lines. I look forward to checking them out, another great post, thanks
Introduction One of the marquee features of APEX 26.1 was AI Interactive Reports. When I started testing this new feature, I was interested to see what was going on behind the scenes. In this post, I

Introduction One of the marquee features of APEX 26.1 is APEX AI Agents. APEX AI Agents allow you to use an LLM and your own PL/SQL and JS tools to perform actions on your data instead of just chattin

Introduction In my previous post, I looked at logging APEX AI Agent requests and responses with Request and Response handlers. Logging is the first step because it shows you what is actually moving th

Introduction Logging is one of the first things you need when building serious AI Agents. Without it, debugging quickly becomes guesswork. You can see the final answer, but not always why the model ch

Context management patterns from building agents in Oracle APEX

This is the fourth and final post in my APEX & Microsoft Office 365 Integration series. This post assumes you have at least read the first two posts in the series APEX & Office 365 Integration - Introduction and APEX & Office 365 Calendar Integration.
This post will discuss integrating MS Office 365 Email with Oracle APEX. Being able to process email from APEX has several potential uses, including:
My Office 365 Demo App, which you can get from my Github Repository, has been updated to include the functionality described in this post, namely:
This section assumes you have read the first post in the series APEX & Office 365 Integration - Introduction. It also assumes that you have already set up a Microsoft Active Directory App registration using 'Microsoft Entra'. Before proceeding, you will need to add the following permissions to your App registration:
Mail.Read (allows you to read email for all users)โ ๏ธ With the Mail.Read permission, the app registration can read anyone's email in the entire organization. Refer to the first post in the series for details on how to limit this access using access policies. For this demo, I created an access policy that restricted my AD App registration to only be able to access my account.
In this section, I will show you how to call the various MS Graph Email APIs we will use to list emails in a user's inbox, and download email attachments.
Please refer to my previous post APEX & Office 365 Integration - Introduction for details on obtaining an MS Graph token using Postman. The token returned from this step will be used as a Bearer Token in the following steps.
https://graph.microsoft.com/v1.0/users/USER_ID/mailFolders/Inbox/messages. Where USER_ID is the user id or userPrincipalName of a specific userFirst, we want to get a list of emails in an Inbox jon@cloudnueva.com:

$select parameter; there are more fields availablebodyPreview contains just the first 255 characters of the body (in plain text)Prefer header in my example returns plaintext for the email body.content (as opposed to html). Being able to return plain text can be useful if you need to parse the email body and take some action based on its contenthttps://graph.microsoft.com/v1.0/users/USER_ID/mailFolders/Inbox/messages/MESSAGE_ID/attachments. Where USER_ID is the user id or userPrincipalName and MESSAGE_ID is the id of the message from the previous request.In the above JSON response, we see that the first email has the value "hasAttachments": true. To get a list of the attachments for the email setup Postman as follows:

$select parametercontentBytes field which contains the actual bytes for the attachment. If you are looking for a list of attachments, the web service runs much faster if you exclude the contentBytes field.https://graph.microsoft.com/v1.0/users/USER_ID/mailFolders/Inbox/messages/MESSAGE_ID/attachments/ATTACHMENT_ID/$value. Where USER_ID is the user id or userPrincipalName, MESSAGE_ID is the id of the message, and
ATTACHMENT_ID is the id of the attachment from the previous request.The final step is to get the attachment BLOB. Setup Postman as per the screenshot below:

/$value appended to the URL; this gets the attachment BLOB instead of details about the attachmentNow that we understand how the MS Graph Email APIs work, we can start integrating them with APEX. I developed a demo APEX App to show how this integration can be done. You can download the demo App from my Github Repository. In this section, I will highlight some critical aspects of the demo App.
We will need to create two REST Data Sources, one to get a list of emails in a particular user's inbox and a second to get a list of attachments for a specific email message.

user_id value; this will help when 'Discovering' the data profile later on
https://login.microsoftonline.com/YOUR_TENANT_ID/oauth2/v2.0/token. Substitute YOUR_TENANT_ID with your MS Office 365 Tenant ID.items


value and click 'Apply Changes'

$select parameter above. Selecting specific fields makes the response smaller and faster

That completes the creation of the REST Data Source to get email messages in a Users Inbox. This REST Data Source is being used on page 40 of the Demo App.
The steps for creating the REST Data Source to list attachments for a given message are very similar to the steps described in the previous section. Instead of going through each step, I have included three necessary screenshots below. You can also look at the REST Data Source 'Office 365 Email Attachments' in the Demo App.
REST Source Definition

REST Source Data Profile

REST Source Parameters

So far in this post, we have used REST Data Sources to handle fetching the inbox and message attachments. To fetch the BLOB for a specific attachment, we will switch to PL/SQL.
The procedure dl_email_attachment fetches an attachment and uses apex_data_export to download it. Here are the essential parts of this procedure:
PROCEDURE dl_email_attachment
(p_user_id IN VARCHAR2,
p_email_id IN VARCHAR2,
p_attachment_id IN VARCHAR2,
p_file_name IN VARCHAR2,
p_mime_type IN VARCHAR2) IS
l_file_size PLS_INTEGER;
l_export apex_data_export.t_export;
l_url VARCHAR2(4000);
BEGIN
-- Build URL with Parameters.
l_url := 'https://graph.microsoft.com/v1.0/users/#USER_ID#/mailFolders/Inbox/messages/#MESSAGE_ID#/attachments/#ATTACHMENT_ID#/$value';
l_url := REPLACE(l_url, '#USER_ID#', p_user_id);
l_url := REPLACE(l_url, '#MESSAGE_ID#', p_email_id);
l_url := REPLACE(l_url, '#ATTACHMENT_ID#', p_attachment_id);
-- Set HTTP Headers
apex_web_service.clear_request_headers;
apex_web_service.set_request_headers
(p_name_01 => 'Content-Type',
p_value_01 => 'application/x-www-form-urlencoded');
-- Call MS Graph Web Service.
-- token_url is a function that build the MS Graph Token URL, see the Demo package for details
-- option_value is s function that gets the name of the APEX Web Credential for our MS Graph Credentials
l_export.content_blob := apex_web_service.make_rest_request_b
(p_url => l_url,
p_http_method => 'GET',
p_token_url => token_url,
p_credential_static_id => option_value (p_option_code => 'WEB_CREDENTIAL_STATIC_ID'));
-- Download the File.
l_export.file_name := p_file_name;
l_export.mime_type := p_mime_type;
apex_data_export.download( p_export => l_export );
END dl_email_attachment;
In this post I showed you how you can take advantage of MS Graph APIs to process emails in Office 365. This functionality provides you with even more options for building APEX applications that interact with your users in ways that are convenient to them.
I hope you enjoyed my series on integrating MS Office 365 with APEX!