How to Set Up SMTP Email Sending in Oracle Cloud(OCI)
Abotts Logo Abotts Partners with singapore based tech giant to help migrate their public sector customer from Sybase to SQL server.
Upworks Logo Abotts successfully decouples and migrates Upwork EBS custom schema to oracle cloud.
Abotts Logo ABOTTS partners with County in Los Angeles to upgrade their court infrastructure into new technologies.
Upworks Logo Upworks Inc partners with ABOTTS to build their Oracle Cloud Infrastructure (OCI) and migrate their custom applications to OCI.
QuinStreet Logo QuinStreet partners with Abotts to archive and manage their IT systems on Oracle cloud (OCI).
Gnorth Logo Abotts Inc Partners with Gnorth consulting to deploy exadata and ODA for a large public sector customer.
Abotts Logo Abotts Partners with singapore based tech giant to help migrate their public sector customer from Sybase to SQL server.
Upworks Logo Abotts successfully decouples and migrates Upwork EBS custom schema to oracle cloud.
Abotts Logo ABOTTS partners with County in Los Angeles to upgrade their court infrastructure into new technologies.
Upworks Logo Upworks Inc partners with ABOTTS to build their Oracle Cloud Infrastructure (OCI) and migrate their custom applications to OCI.
QuinStreet Logo QuinStreet partners with Abotts to archive and manage their IT systems on Oracle cloud (OCI).
Gnorth Logo Abotts Inc Partners with Gnorth consulting to deploy exadata and ODA for a large public sector customer.

How to Set Up SMTP Email Sending in Oracle Cloud Infrastructure (OCI)

 

Step 1: Configure an SMTP Connection

Before you can send emails, you need to configure an SMTP connection through the OCI Console. This is the foundational step in setting up email delivery.

  1. Open the Navigation Menu:
    • In the OCI Console, click the navigation menu (three horizontal lines on the top-left of the screen).
    • Under Developer Services, select Email Delivery.
  2. Access SMTP Configuration:
    • In the Email Delivery section, click on Configuration.
    • Here you will find the SMTP sending information panel, which provides the following key details:

         A. Public Endpoint:

    • This is the public endpoint you will use to send emails, specific to your region.

         B. SMTP Ports:

    • Email Delivery supports two primary ports for SMTP connections:
      • Port 25 for non-secure SMTP connections
      • Port 587 for secure TLS-encrypted connections (recommended).

        C. Security:

    • Ensure that TLS encryption is enabled for secure transmission of emails. It’s a security best practice to encrypt emails while in transit to the Oracle Cloud Infrastructure Email Delivery service.

 

Step 2: Create SMTP Credentials

To authenticate your email-sending requests, you must create SMTP credentials. These credentials are tied to your OCI account or a specific user.

  1. View User Details:
    • If you’re generating SMTP credentials for your own user, click on the Profile menu (top-right corner of the navigation bar), then click User Settings.
    • If you’re an administrator generating credentials for another user, navigate to Identity & Security > Users in the OCI console. Find the desired user and click on their name to view their details.
  2. Generate SMTP Credentials:
    • In the user details page, look for the SMTP Credentials section. Follow the prompts to generate a set of SMTP credentials, which you will use for authenticating the email sending.

 

Step 3: Create an Approved Sender for OCI SMTP Email Sender

Before sending emails, you need to specify an approved sender within OCI. This step ensures that the email addresses you use are authorized to send emails via OCI.

  1. Navigate to Approved Senders:
    • Go to Developer Services > Email Delivery in the navigation menu.
    • Under Email Delivery, click on Approved Senders.
  2. Create an Approved Sender:
    • Click Create Approved Sender.
    • In the Create Approved Sender dialog, enter the email address you want to use as the approved sender.
    • (Optional) Add tags to help organize resources within OCI.
    • Click Create Approved Sender to save the email address.

 

Step 4: Allow SMTP Access for Admin User by Appending an Access Control Entry (ACE)

For security reasons, you must configure access control to allow the admin user to send emails via SMTP. This is done by appending an Access Control Entry (ACE).

Run the following PL/SQL script to append an ACE that allows SMTP access:

BEGIN
— Allow SMTP access for user ADMIN
DBMS_NETWORK_ACL_ADMIN.APPEND_HOST_ACE(
host => ‘smtp.us-ashburn-1.oraclecloud.com’,
lower_port => 587,
upper_port => 587,
ace => xs$ace_type(privilege_list => xs$name_list(‘SMTP’),
principal_name => ‘ADMIN’,
principal_type => xs_acl.ptype_db));
END;
/

This script ensures that your ADMIN user can send emails via the SMTP server.

 

Step 5: Create a PL/SQL Procedure to Send Emails

Now that your SMTP connection is configured, it’s time to create a PL/SQL procedure to send emails programmatically. This procedure will utilize Oracle’s UTL_SMTP package to send emails.

CREATE OR REPLACE PROCEDURE SEND_MAIL (

  msg_to varchar2,

  msg_subject varchar2,

  msg_text varchar2 ) 

IS

  mail_conn utl_smtp.connection;

  username varchar2(1000):= ‘ocid1.user.oc1.username’;

  passwd varchar2(50):= ‘password’;

  msg_from varchar2(50) := ‘adam@example.com’;

  mailhost VARCHAR2(50) := ‘smtp.us-ashburn-1.oraclecloud.com’;

 

BEGIN

  mail_conn := UTL_smtp.open_connection(mailhost, 587);

  utl_smtp.starttls(mail_conn);

  — Authenticate with SMTP server

  UTL_SMTP.AUTH(mail_conn, username, passwd, schemes => ‘PLAIN’);

 

  — Send the email

  utl_smtp.mail(mail_conn, msg_from);

  utl_smtp.rcpt(mail_conn, msg_to);

 

  UTL_smtp.open_data(mail_conn);

  UTL_SMTP.write_data(mail_conn, ‘Date: ‘ || TO_CHAR(SYSDATE, ‘DD-MON-YYYY HH24:MI:SS’) || UTL_TCP.crlf);

  UTL_SMTP.write_data(mail_conn, ‘To: ‘ || msg_to || UTL_TCP.crlf);

  UTL_SMTP.write_data(mail_conn, ‘From: ‘ || msg_from || UTL_TCP.crlf);

  UTL_SMTP.write_data(mail_conn, ‘Subject: ‘ || msg_subject || UTL_TCP.crlf);

  UTL_SMTP.write_data(mail_conn, ‘Reply-To: ‘ || msg_to || UTL_TCP.crlf || UTL_TCP.crlf);

  UTL_SMTP.write_data(mail_conn, msg_text || UTL_TCP.crlf || UTL_TCP.crlf);

 

  UTL_smtp.close_data(mail_conn);

  UTL_smtp.quit(mail_conn);

 

EXCEPTION

  WHEN UTL_smtp.transient_error OR UTL_smtp.permanent_error THEN

    UTL_smtp.quit(mail_conn);

    dbms_output.put_line(sqlerrm);

  WHEN OTHERS THEN

    UTL_smtp.quit(mail_conn);

    dbms_output.put_line(sqlerrm);

END;

/

In this procedure:

  • msg_to: The recipient’s email address.
  • msg_subject: The subject of the email.
  • msg_text: The body content of the email.

 

Step 6: Send a Test Email to Validate the Setup

Finally, you should send a test email to ensure that your configuration is correct. Execute the following PL/SQL statement:

execute send_mail(‘my@example.com’, ‘Email from Oracle Autonomous Database’, ‘Sent using UTL_SMTP’);

If everything is set up correctly, you should receive an email in the specified recipient’s inbox.

 

 

External Link Suggestions: