Sunday, January 23, 2011

How to send email from 10g Oracle Database (UTL_MAIL)?

To enable mailing from database using below steps.
1. sqlplus ‘/ as sysdba’
2. @$ORACLE_HOME/rdbms/admin/utlmail.sql
3. @$ORACLE_HOME/rdbms/admin/prvtmail.plb
4. Set smtp_server information in init.ora or spfile.ora

alter system set smtp_out_server = ‘SMTP_SERVER_IP_ADDRESS:SMTP_PORT’ scope=both;

Use default SMTP Port as 25.
If database instance had been started with spfile

alter system set smtp_out_server = ’10.10.10.10:25′ scope=both;

If database instance had been started with pfile


alter system set smtp_out_server = ’10.10.10.10:25′;


Also make below entry in your initSID.ora 
smtp_out_server = ’10.10.10.10:25′

To enable other DB users to use this functionality, grant execute permission on UTL_MAIL package.

grant execute on utl_mail to USERS;

Now,database configuration finished.
To send email, use below code in your procedure/package,

exec utl_mail.send((sender => ‘abc@abc.com’, recipients => ‘xyz@xyz.com’, subject => ‘Testing UTL_MAIL Option’, message => ‘TESTMAIL USING  UTL_MAIL PACKAGE.’);
 
Check the inbox of the email id, to verify the email received or not.