Sie sind auf Seite 1von 6

Sending Mail from an 11G Oracle Database (PIPE) By Adnan Khan

Oracle 11g first introduced the utl_mail package, which provides a simpler and more intuitive email API
through oracle pipe. The package is loaded by running the following scripts as the SYSUSER

SQL> CONN SYS USER AS SYSDBA

Set the below parameter

SQL> Alter system set smtp_out_server='smtp.mycompany.com:25 scope=both';------Need Change


SQL> shutdown immediate
SQL> startup

Run the below scripts

1- SQL> @ORACLE_HOME/rdbms/admin/utlmail.sql
2- SQL>@ORACLE_HOME/rdbms/admin/prvtmail.plb
3- SQL> grant execute on utl_mail TO public;

With the configuration complete, it is now possible to send an email using the send
procedure.

BEGIN
UTL_MAIL.SEND (SENDER

=>Sender email address,

RECIPIENTS

=> Receipt Email Address,

SUBJECT

=> Test utl_mail.send procedure,

MESSAGE

=>

If you are reading this it worked!);

END;

The code related to the job needs to be placed into a database procedure which captures errors using an
exception handler and sends the appropriate email. The following procedure is the Oracle 11g
equivalent of the one used in the utl_smtp example.

Automated Email Alert in 11g

CREATE OR REPLACE PROCEDURE automated_email_alert AS

V_mail_host
V_sender

Varchar2 (50) := smtp.mycompany.com;------Need Change

Varchar2 (50) := adnan.khan@shajarcapital.com;------Need Change

V_to

Varchar2 (50) := db4ora@gmail.com------Need Change

BEGIN
Dbms_stats.gather_database_stats (cascade

=> true, options

=>? gather auto?);

Utl_mail.send (Sender =>V_sender, Recipients =>V_to, Subject=>Success, Message=>Completed


Successfully!);

EXCEPTION
When others then
Utl_mail.send (Sender =>V_sender, Recipients =>V_to, Subject=>Error, Message=>Failed: ||
sqlerrm);

END;

2ndMethod for sending Email

In order to send mails in Oracle 11g you will need to take care of several steps.
They are not default installed in the database (11g). You need to actually install the UTL_MAIL package.

SQL> CONN SYS USER AS SYSDBA

Set the below parameter

SQL> Alter system set smtp_out_server='smtp.mycompany.com:25 scope=both';------Need Change


SQL> shutdown immediate
SQL> startup

Run the below scripts

SQL> @ORACLE_HOME/rdbms/admin/utlmail.sql
SQL> @ORACLE_HOME/rdbms/admin/prvtmail.plb
SQL> grant execute on utl_mail TO public;

Check what is out there in the database.


SQL> SELECT * from dba_network_acls;

Dropping same named access_control list if already existing.


BEGIN
DBMS_NETWORK_ACL_ADMIN.DROP_ACL (acl => mail_access.xml);
END;

Create an access control list:

BEGIN
DBMS_NETWORK_ACL_ADMIN.CREATE_ACL (acl

=> mail_access.xml,

Description => Permissions to access e-mail server.,


Principal

=> PUBLIC,
Is_grant

=> true,

Privilege

=> connect);

END;

Assign the list to the SMTP(mailserver).


## Note Default port is 25!
BEGIN
DBMS_NETWORK_ACL_ADMIN.ASSIGN_ACL (
acl

=> mail_access.xml,

host

=> smtp address of company,------Need Change

lower_port

=> 25,

upper_port

=> 25);

Commit;
END;

This is an example to check if it is working:

CREATE OR REPLACE PROCEDURE Send_mail


(p_to varchar2, p_from varchar2, p_message varchar2, p_host varchar2, p_port number default 25)

AS
L_mail_conn UTL_SMTP. Connection;
BEGIN

L_mail_conn:= UTL_SMTP.open_connection (p_host,p_port);


UTL_SMTP.helo (l_mail_conn, p_host);
UTL_SMTP.mail (l_mail_conn, p_from);
UTL_SMTP.rcpt (l_mail_conn, p_host);
UTL_SMTP.data (l_mail_conn, p_message || UTL_TCP.crlf || UTL_TCP.crlf);
UTL_SMTP.quit (l_mail_conn);
END;

The code below shows how the procedure is called.


BEGIN
Send_mail (p_to

END;

=> db4ora@gmail.com,------Need Change

P_from

=> adnan.khan@shajarcapital.com,------Need Change

P_message

=> This is a Test message.

P_host

=> smtp address.com);------Need Change

Das könnte Ihnen auch gefallen