Categories: PLSQL Tutorials

How to send mail using UTL_MAIL package in PL/SQL?

In my previous article I have given the details about multiple types of triggers with its real life industry examples. In this article I would like to give information about another important topic which has been used to send the mails in PL/SQL program. There are so many times requirements to send the mail after updates,inserts or deletion of records. These kind of all settings will be handled by oracle in built package UTL_MAIL package which will provide the way to send the mail through oracle engine. The mail is always sending using SMTP protocol – Simple Mail Transfer Protocol.

What you will see in this article?

1.What is UTL_MAIL package and its multiple types?

2.How to send mail using oracle engine and SMTP protocol?

What is UTL_MAIL package and its multiple types?

The UTL_MAIL package is the utility which is used to manage the sending mail using oracle and SMTP protocol. The UTL_MAIL package is very important package to manage the mails. There are so many requirements in real world which needs to use this package.

Initial Steps to use UTL_MAIL package :

Step 1 : Install the UTL_MAIL package

The first step is to install UTL_MAIL package. Just remember that the following is path of SQL files. DBA need to install this package using following path.

@$ORACLE_HOME/rdbms/admin/utlmail.sql
@$ORACLE_HOME/rdbms/admin/prvtmail.plb

Step 2 : SMTP_OUT_SERVER parameter set up

The second step is to set up the SMTP_OUT_SERVER parameter. The DBA team needs to set this parameter and add the SMTP details to this parameter.

Step 3 : DBA needs to add the privileges’

We require to add and give the specified privileges to the User to execute this package.

Step 4 : Add the ACL record

The DBA need to add Record in ACL (Access Control List) using package called DBMS_NETWORK_ACL_ADMIN

Step 5 : Understanding different procedures of UTL_MAIL package

Now with using above steps user can use the UTL_MAIL package. There are more procedures of UTL_MAIL package. Here we will see with example how to use UTL_mail package with multiple procedures.

UTL_MAIL Procedures

These are above three different procedures for sending the mail in PL/SQL. How to use that we will see with example in next section.

How to send mail using oracle engine and SMTP protocol?

In this section we will see the detailed steps and example of sending mail using oracle engine. I am taking example on my system and will try to show you stepwise how to add this feature. Most of the time this kind of feature is useful to fulfill business needs. If you are using ERP system and creating the account. After creating the account user will get the mail. This is most simple example for sending the mail

Real life example :

Step 1 : In order to send the mail we require to install UTL_MAIL package

Connect to Sysdba

Sys as sysdba

execute the following SQL which is on pluggable databases (I am taking path on my system)

@C:\app\Amit\product\12.2.0\dbhome_1\rdbms\admin\utlmail.sql
@C:\app\Amit\product\12.2.0\dbhome_1\rdbms\admin\prvtmail.plb

Step 2 : Give the grants to package

GRANT execute ON utl_mail TO public;

Step 3 : Add record to ACL

The DBMS_NETWORK_ACL_ADMIN package provides the interface to administer
The network Access Control List (ACL).

Step 4 : Set smtp_out_server parameter

alter system set smtp_out_server = ‘mail.xxx.ae.ac:33’;

Step 5 : Procedure creation and use the package

Syntax :

BEGIN
UTL_MAIL.send(sender => ‘complexsql@gmail.com’,
recipients => ‘complexsql@hotmail.com’,
subject => ‘This is Test mail’,
message => ‘Hello World’,
mime_type => ‘text; charset=us-ascii’);
END;

Using the above steps you can send the mail .

Scenario 2 : If you want to send image attached mail Kindly refer following syntax.

RAW Mail

Example :

—Create function to get the image

Create
function get_image (P_DIR VARCHAR2,P_FILE_NAME VARCHAR2)
RETURN RAW
IS
IMAGE raw(32767);
f bfile :=bfilename(P_DIR,P_FILE_NAME);
–BFILENAME returns a BFILE locator that is associated
–with a physical LOB binary file on the server file system
BEGIN
–This procedure opens a BFILE for read-only access.
dbms_lob.fileopen(f,dbms_lob.file_readonly);
IMAGE:= dbms_lob.substr(f);
dbms_lob.close(f);
return IMAGE;
END;

–Send mail
BEGIN
utl_mail.send_attach_raw(sender => ‘complexsql@gmail.com’,
recipients => ‘complexsql@hotmail.com’,
subject => ‘Test Mail with Attachment of image’,
message => ‘kindly find the attachment’,
mime_type => ‘text; charset=us-ascii’,
attachment=> get_image(‘AMIT’,’Amit.jpg’), —calling the function
att_inline=>true,
att_mime_type=>’image/jpg’,
att_filename=>’Amit.jpg’
);
END;

With using the same process you can send attachment. If you want more details on it kindly comment in comments section. If you like this article or if you have any issues with the same kindly comment in comments section.

Amit S

Oracle Consultant with vast experience in Oracle BI and PL/SQL Development. Amiet is the admin head of this website who contributes by preparing tutorials and articles related to database technologies. He is responsible to manage the content and front-end of the website.

Share
Published by
Amit S

Recent Posts

What is Root Cause Analysis (RCA) With real examples

In my previous article I have given details about application support engineer day to day…

4 weeks ago

Application Support Engineer Day to day responsibilities

In my previous articles I have given the roles and responsibilities of L1,L2 and L3…

1 month ago

What is mean by SLA ( Service Level Agreement) with Examples?

In my previous articles i have given the hierarchy of production support in real company…

1 month ago

What is Production support Hierarchy in organization?

In this article i would like to provide information about production support organization structure or…

1 month ago

What are roles and responsibilities for L3 Support Engineer?

In my previous article I have given roles for L1 and L2 support engineer with…

1 month ago

What are roles and responsibilities of L2 Engineer?

I have started this new series of how to become application support engineer. This article…

1 month ago