Search This Blog

Tuesday, July 8, 2025

How Do I Send Emails in Oracle APEX: Available Methods and Best Practices

Introduction
Sending emails is a fundamental feature in many Oracle APEX applications, enabling communication through notifications, alerts, confirmations, and more. Oracle APEX provides multiple methods for sending emails, ranging from built-in packages like APEX_MAIL to integration with external SMTP servers and third-party services. Understanding these methods and following best practices ensures reliable, secure, and efficient email delivery in your applications.

 In Oracle APEX, sending emails is a common requirement for applications that need to notify users, send confirmations, alerts, or reports. There are several methods available to send emails, each suited to different scenarios and infrastructure setups. Understanding these methods and following best practices is essential for reliable and secure email delivery.

Available Methods to Send Emails in Oracle APEX:

  1. APEX_MAIL Package
    This is the built-in PL/SQL package provided by Oracle APEX for sending emails directly from your database. It allows sending plain text or HTML emails, adding attachments, and controlling email headers. The APEX_MAIL package relies on SMTP configuration set up at the APEX instance or database level.
    Example to send a simple email:

    BEGIN
      APEX_MAIL.SEND(
        p_to   => 'recipient@example.com',
        p_from => 'noreply@yourdomain.com',
        p_subj => 'Sample Email',
        p_body => 'This is a test email from Oracle APEX.'
      );
      APEX_MAIL.PUSH_QUEUE;
    END;
    
  2. UTL_SMTP Package
    A lower-level PL/SQL package that interacts directly with SMTP servers. This requires more manual handling of SMTP commands but offers flexibility for customized email sending logic. Use this when you need more control or when APEX_MAIL is not available or suitable.

  3. External Email APIs and Web Services
    Integrate Oracle APEX with third-party email delivery services such as SendGrid, Amazon SES, or Microsoft Graph API. This is often done via RESTful web services, allowing you to send emails using modern APIs and benefit from advanced features like analytics, templates, and high deliverability.

  4. Oracle Cloud Infrastructure Email Delivery
    If your APEX application runs on Oracle Cloud, you can configure SMTP to use OCI Email Delivery, a secure and scalable managed SMTP service. This reduces maintenance overhead and increases email deliverability.

Best Practices for Sending Emails in Oracle APEX:

  • Configure SMTP Properly
    Ensure your SMTP server is correctly configured with authentication, SSL/TLS security, and the appropriate port settings. Use approved sender addresses and configure SPF, DKIM, and DMARC DNS records for your domain to avoid email being marked as spam.

  • Use Approved Sender Addresses
    Always send emails from addresses authorized by your SMTP provider or domain to maintain deliverability and compliance.

  • Handle Email Queue Efficiently
    Use APEX_MAIL.PUSH_QUEUE to process pending emails promptly. Monitor mail queues and logs via APEX_MAIL_QUEUE and APEX_MAIL_LOG views to detect and fix delivery issues early.

  • Implement Error Handling
    Capture exceptions in your PL/SQL email sending code and log errors for troubleshooting. This prevents silent failures and aids in debugging.

  • Limit Bulk Emailing
    For bulk mailing, consider using external services optimized for high volume email delivery to avoid overloading your database or SMTP server and reduce the risk of blacklisting.

  • Test Thoroughly
    Always test email functionality in development with controlled recipient addresses and use test mailboxes or mail traps to avoid sending unintended emails.

By selecting the appropriate method for your application’s context and adhering to best practices, you can implement robust email functionality in Oracle APEX. This improves user engagement, automates communication, and enhances the overall effectiveness of your applications.

Sending emails is a common requirement in Oracle APEX applications, whether for notifications, system alerts, or user interactions. Oracle APEX provides multiple ways to send emails, each suited for different use cases. Understanding these methods helps developers implement reliable and efficient email functionality.

This tutorial covers all the ways to send emails in Oracle APEX, their use cases, advantages, and examples.


Ways to Send Emails in Oracle APEX

Oracle APEX supports four primary ways to send emails:

  1. Using the APEX_MAIL Package

  2. Using the APEX_SMTP Package

  3. Using Oracle Cloud Infrastructure (OCI) Email Delivery

  4. Using Third-Party APIs via Web Services

Each method is useful depending on requirements such as email format, attachments, external SMTP providers, and cloud services.


1. Sending Emails Using the APEX_MAIL Package

Overview

The APEX_MAIL package is the most commonly used method for sending emails in Oracle APEX. It provides built-in email handling capabilities, supports plain-text and HTML emails, allows attachments, and requires minimal configuration.

Use Cases

  • System-generated emails (password resets, user notifications)

  • Emails with attachments

  • Emails with multiple recipients (To, CC, BCC)

  • HTML-formatted emails

How to Send an Email Using APEX_MAIL

BEGIN

    APEX_MAIL.SEND (

        p_from      => 'admin@example.com',

        p_to        => 'user@example.com',

        p_cc        => 'manager@example.com',

        p_bcc       => 'audit@example.com',

        p_subj      => 'Account Activation',

        p_body      => 'Your account has been activated.',

        p_body_html => '<h3>Welcome!</h3><p>Your account has been activated.</p>'

    );


    -- Push the email to be processed

    APEX_MAIL.PUSH_QUEUE;

    COMMIT;

END;

Key Features

  • Supports plain-text and HTML emails

  • Allows attachments using APEX_MAIL.ADD_ATTACHMENT

  • Supports multiple recipients

  • Queued emails must be processed using APEX_MAIL.PUSH_QUEUE

Best Practices

  • Always call APEX_MAIL.PUSH_QUEUE to ensure emails are sent.

  • Use HTML formatting to improve email appearance.

  • Validate that email addresses are correct before sending.


2. Sending Emails Using the APEX_SMTP Package

Overview

APEX_SMTP provides low-level email handling and requires direct interaction with an SMTP server. Unlike APEX_MAIL, it does not queue emails; instead, emails are sent immediately.

Use Cases

  • Sending emails without relying on APEX mail queues

  • Connecting to custom SMTP servers outside of Oracle Cloud

  • Sending emails with precise SMTP control

How to Send an Email Using APEX_SMTP

DECLARE

    v_conn APEX_SMTP.CONNECTION;

BEGIN

    -- Establish SMTP connection

    v_conn := APEX_SMTP.INITIALIZE('smtp.example.com', 587);

    APEX_SMTP.HELO(v_conn, 'example.com');

    APEX_SMTP.MAIL(v_conn, 'admin@example.com');

    APEX_SMTP.RCPT(v_conn, 'user@example.com');

    

    -- Write email content

    APEX_SMTP.OPEN_DATA(v_conn);

    APEX_SMTP.WRITE_DATA(v_conn, 'Subject: Test Email' || APEX_LF || APEX_LF);

    APEX_SMTP.WRITE_DATA(v_conn, 'Hello, this is a test email.');

    APEX_SMTP.CLOSE_DATA(v_conn);

    

    -- Terminate SMTP session

    APEX_SMTP.QUIT(v_conn);

END;

Key Features

  • Requires a direct SMTP server connection

  • Supports immediate email sending (no queue processing)

  • Provides low-level SMTP control

Best Practices

  • Ensure the SMTP server supports authentication.

  • Use APEX_SMTP.WRITE_DATA to format email headers correctly.

  • Be mindful of SMTP security configurations (TLS/SSL).


3. Sending Emails Using OCI Email Delivery

Overview

Oracle Cloud Infrastructure (OCI) Email Delivery is Oracle's recommended method for sending emails from APEX in cloud environments. It provides a scalable and reliable way to send transactional and marketing emails.

Use Cases

  • Sending emails from Oracle APEX on Autonomous Database

  • Ensuring high deliverability and compliance with email providers

  • Sending bulk emails

How to Send an Email Using OCI Email Delivery

Step 1: Configure SMTP Settings

  1. Identify the SMTP connection endpoint from Oracle Cloud Console.

  2. Generate SMTP credentials for authentication.

  3. Create approved senders in Oracle Cloud Console.

  4. Set up the SMTP parameters in APEX instance settings:

BEGIN

    APEX_INSTANCE_ADMIN.SET_PARAMETER('SMTP_HOST_ADDRESS', 'smtp.us-phoenix-1.oraclecloud.com');

    APEX_INSTANCE_ADMIN.SET_PARAMETER('SMTP_USERNAME', 'ocid1.user.oc1.username');

    APEX_INSTANCE_ADMIN.SET_PARAMETER('SMTP_PASSWORD', 'your_password');

    COMMIT;

END;

Step 2: Send Email Using APEX_MAIL

BEGIN

    APEX_MAIL.SEND (

        p_from      => 'admin@yourdomain.com',

        p_to        => 'user@example.com',

        p_subj      => 'Welcome to Our Service',

        p_body      => 'Thank you for signing up!',

        p_body_html => '<h3>Welcome!</h3><p>Thank you for signing up.</p>'

    );


    -- Push email queue

    APEX_MAIL.PUSH_QUEUE;

    COMMIT;

END;

Key Features

  • Requires an Oracle Cloud Infrastructure (OCI) account

  • Supports email authentication (SPF, DKIM)

  • Better deliverability than standard SMTP solutions

Best Practices

  • Configure OCI Email Delivery before using it in APEX.

  • Ensure that email addresses are on the approved sender list.


4. Sending Emails Using Third-Party APIs via Web Services

Overview

If your APEX application needs to send emails via services like SendGrid, Mailgun, or Amazon SES, you can use web service calls to interact with these providers' APIs.

Use Cases

  • Sending emails from external providers

  • Using advanced email marketing features

  • Tracking email opens and clicks

How to Send an Email Using a REST API

DECLARE

    v_clob CLOB;

BEGIN

    v_clob := '{ "personalizations": [ { "to": [ { "email": "user@example.com" } ] } ], "from": { "email": "admin@example.com" }, "subject": "Test Email", "content": [ { "type": "text/plain", "value": "Hello!" } ] }';


    apex_web_service.make_rest_request (

        p_url         => 'https://api.sendgrid.com/v3/mail/send',

        p_http_method => 'POST',

        p_body        => v_clob,

        p_username    => 'Bearer YOUR_SENDGRID_API_KEY',

        p_parm_name   => apex_t_varchar2('Content-Type'),

        p_parm_value  => apex_t_varchar2('application/json')

    );

END;

Key Features

  • Provides full control over email formatting

  • Supports tracking and analytics

  • Requires API authentication and configuration

Best Practices

  • Ensure the API key is secure and not exposed in the code.

  • Use error handling to handle API failures.


Oracle APEX provides multiple ways to send emails, each with its advantages. For basic emails, use APEX_MAIL. For custom SMTP control, use APEX_SMTP. For cloud environments, OCI Email Delivery is ideal. For external email services, use web APIs.

By choosing the right method based on your requirements, you can ensure efficient and reliable email communication in your APEX applications.



 

Conclusion
By leveraging the available email sending methods in Oracle APEX and adhering to best practices such as proper authentication, secure SMTP configuration, and error monitoring, developers can create robust applications with seamless email communication. This enhances user experience and ensures important messages reach recipients promptly and securely.

 

How Do I Set Up Email in the Oracle APEX IDE

 

Introduction
Setting up email in the Oracle APEX Integrated Development Environment (IDE) is a key step to enable your applications to send notifications, alerts, and other automated messages. Configuring email within the APEX IDE allows developers to test and implement email functionality seamlessly during application development. This setup involves specifying SMTP settings and using built-in APEX packages to handle email delivery.

To set up email in the Oracle APEX Integrated Development Environment (IDE), you need to configure the SMTP settings so that your applications can send emails such as notifications, alerts, or confirmations directly from the development environment. This setup enables developers to test email functionality during development and ensures smooth email communication once the application is deployed.

Here is a detailed guide on how to set up email in the Oracle APEX IDE:

  1. Access the APEX Instance Administration
    Log in to your Oracle APEX workspace with an account that has administrative privileges. From the APEX home page, navigate to Manage Instance or Instance Administration.

  2. Navigate to Email Configuration Settings
    Within the administration interface, find the Instance Settings or Manage Instance Settings section. Scroll down to the Email subsection where SMTP parameters are configured.

  3. Enter SMTP Server Details

    • SMTP Host Address: Enter the hostname or IP address of the SMTP server that will relay your emails. This could be an internal mail server or an external SMTP service such as Gmail SMTP or Oracle Cloud Infrastructure Email Delivery.

    • SMTP Port: Specify the port number used by the SMTP server. Common ports are 587 for TLS or 465 for SSL connections.

  4. Provide Authentication Credentials

    • Enter the SMTP Username and Password required to authenticate to the SMTP server. This ensures secure sending of emails and prevents unauthorized use of the mail server.

  5. Configure Security Settings

    • Enable SSL or TLS/STARTTLS according to your SMTP server’s security requirements. For most modern SMTP services, STARTTLS on port 587 is standard.

  6. Set the Default Sender Email Address
    Specify a valid email address that will appear as the From address on outgoing emails. This email should be authorized to send emails through your SMTP server.

  7. Save the Configuration
    Once all parameters are entered, save or apply the changes. This configures the APEX instance to use the provided SMTP settings when sending emails.

  8. Test the Email Setup
    Use the APEX SQL Workshop or a dedicated test page to execute a simple PL/SQL block that sends a test email. Example:

    BEGIN
      APEX_MAIL.SEND(
        p_to   => 'testuser@example.com',
        p_from => 'noreply@yourdomain.com',
        p_subj => 'APEX IDE Email Setup Test',
        p_body => 'This is a test email sent from Oracle APEX IDE.'
      );
      APEX_MAIL.PUSH_QUEUE;
    END;
    

    Verify that the email is received in the recipient’s inbox.

  9. Monitor Email Queue and Logs
    Check the views APEX_MAIL_QUEUE and APEX_MAIL_LOG to monitor the status of sent emails, troubleshoot errors, or verify successful delivery.

  10. Additional Considerations

    • Use secure credentials and avoid storing sensitive information in plain text or code repositories.

    • Ensure your SMTP server allows relaying from your APEX server’s IP address.

    • Configure SPF and DKIM DNS records to improve email deliverability and reduce spam classification.

By following these steps, you set up email functionality within the Oracle APEX IDE, allowing developers to test and validate email features directly in the development environment. This ensures that your applications are ready to send reliable and secure emails once deployed.

Oracle APEX provides built-in functionality for sending emails from applications. This feature is essential for notifications, alerts, and confirmations. Before sending emails, the APEX environment must be configured correctly. This tutorial explains the steps to set up email in Oracle APEX IDE, including configuring email settings, using the APEX_MAIL package, and troubleshooting email delivery.


Step 1: Accessing Email Configuration in APEX IDE

Email settings must be configured at the Instance Level in Oracle APEX. These settings define how emails are sent from the application.

  1. Log in to APEX Administration Services

    • Open Oracle APEX in a web browser.

    • Log in as an Instance Administrator.

  2. Navigate to Email Settings

    • Click Manage Instance from the Administration menu.

    • Select Instance Settings.

    • Scroll down to find the Email section.

  3. Configure SMTP Settings

    • Enter the SMTP Host Address (email server address).

    • Specify the SMTP Authentication Credentials if required.

    • Set the SMTP Port (587 for TLS, 465 for SSL, or 25 for non-secure).

  4. Save the settings and restart APEX to apply changes.


Step 2: Configuring SMTP Settings in Autonomous Database

If using Autonomous Database (ADB), email must be configured using Oracle Cloud Infrastructure (OCI) Email Delivery.

  1. Identify the SMTP Endpoint

    • Log in to the OCI Console.

    • Navigate to Email Delivery under Developer Services.

    • Copy the SMTP Host Address.

  2. Generate SMTP Credentials

    • In OCI Console, go to Identity & Security.

    • Select Users, then generate new SMTP Credentials.

    • Save the SMTP username and password.

  3. Set SMTP Configuration in APEX IDE

Execute the following SQL script in SQL Commands in APEX IDE:

BEGIN

    APEX_INSTANCE_ADMIN.SET_PARAMETER('SMTP_HOST_ADDRESS', 'smtp.us-phoenix-1.oraclecloud.com');

    APEX_INSTANCE_ADMIN.SET_PARAMETER('SMTP_USERNAME', 'ocid1.user.oc1.username');

    APEX_INSTANCE_ADMIN.SET_PARAMETER('SMTP_PASSWORD', 'your_smtp_password');

    COMMIT;

END;

  1. Verify SMTP Settings

Run this query to confirm email settings:

SELECT * FROM APEX_INSTANCE_PARAMETERS WHERE parameter_name LIKE 'SMTP%';


Step 3: Sending an Email Using APEX_MAIL in APEX IDE

After setting up SMTP, emails can be sent using the APEX_MAIL package.

Example: Sending a Simple Email

  1. Open the SQL Workshop in APEX IDE.

  2. Click SQL Commands and enter the following PL/SQL block:

BEGIN

    APEX_MAIL.SEND (

        p_from      => 'admin@example.com',

        p_to        => 'user@example.com',

        p_subj      => 'Welcome to Oracle APEX',

        p_body      => 'Hello, your account has been created!'

    );


    -- Push the email queue for processing

    APEX_MAIL.PUSH_QUEUE;

    COMMIT;

END;

Key Points:

  • p_from: The sender's email address (must be an approved sender).

  • p_to: The recipient's email address.

  • p_subj: The email subject line.

  • p_body: The email content (plain text).

  • APEX_MAIL.PUSH_QUEUE: Ensures the email is sent immediately.


Step 4: Sending HTML Emails in APEX IDE

Oracle APEX allows sending emails in HTML format for better presentation.

Example: Sending an HTML Email

BEGIN

    APEX_MAIL.SEND (

        p_from      => 'admin@example.com',

        p_to        => 'user@example.com',

        p_subj      => 'Welcome!',

        p_body_html => '<h3>Welcome to APEX</h3><p>Your account has been created.</p>'

    );


    APEX_MAIL.PUSH_QUEUE;

    COMMIT;

END;

Best Practices for HTML Emails

  • Use proper HTML formatting in p_body_html.

  • Avoid using external CSS; instead, use inline styles.

  • Include both plain text (p_body) and HTML (p_body_html) for better email compatibility.


Step 5: Attaching Files to Emails in APEX IDE

APEX allows sending attachments using APEX_MAIL.ADD_ATTACHMENT.

Example: Sending an Email with a PDF Attachment

DECLARE

    l_blob BLOB;

BEGIN

    -- Fetch a file from the database

    SELECT document_blob INTO l_blob FROM documents WHERE id = 101;


    -- Send email

    APEX_MAIL.SEND (

        p_from      => 'admin@example.com',

        p_to        => 'user@example.com',

        p_subj      => 'Invoice Attached',

        p_body      => 'Please find your invoice attached.'

    );


    -- Attach file

    APEX_MAIL.ADD_ATTACHMENT (

        p_mail_id      => APEX_MAIL.LAST_MAIL_ID,

        p_attachment   => l_blob,

        p_filename     => 'invoice.pdf',

        p_mime_type    => 'application/pdf'

    );


    APEX_MAIL.PUSH_QUEUE;

    COMMIT;

END;


Step 6: Troubleshooting Email Issues in APEX IDE

If emails are not sent successfully, follow these troubleshooting steps:

1. Check Mail Log

SELECT mail_id, sent_date, delivery_status, error_message 

FROM APEX_MAIL_LOG

ORDER BY sent_date DESC;

2. Check Mail Queue

SELECT * FROM APEX_MAIL_QUEUE;

If emails are stuck in the queue, manually push them:

BEGIN

    APEX_MAIL.PUSH_QUEUE;

    COMMIT;

END;

3. Verify SMTP Configuration

SELECT * FROM APEX_INSTANCE_PARAMETERS WHERE parameter_name LIKE 'SMTP%';

Ensure the SMTP settings are correct.


Step 7: Best Practices for Email Setup in APEX IDE

  • Use Verified Senders: Ensure all sender emails are approved in OCI Email Delivery.

  • Monitor Email Logs: Regularly check APEX_MAIL_LOG to track errors.

  • Optimize HTML Emails: Use inline styles and test across email clients.

  • Enable SPF and DKIM: Improve email deliverability by configuring SPF and DKIM records in DNS.

  • Validate Email Addresses: Ensure correct email formats before sending.


Setting up email in Oracle APEX IDE involves configuring SMTP settings, enabling APEX_MAIL, and troubleshooting any delivery issues. With proper setup, APEX applications can send emails for notifications, alerts, and user communications efficiently.

By following these steps, email integration in APEX is reliable, secure, and optimized for best performance.


IDE Setup

  • You have to connect APEX to an email client (not sure how).

  • You need to have the following set up.

A screenshot of a computer

AI-generated content may be incorrect.

Conclusion
By properly setting up email in the Oracle APEX IDE, developers can ensure that their applications communicate effectively with users through email. This capability enhances user engagement and application functionality by automating critical messaging tasks, all while allowing easy testing and debugging during the development process.

UI Defaults

 In Oracle APEX, User Interface (UI) Defaults are a set of metadata-driven, table- and column-scoped attributes that APEX consults when it g...