Send Emails From an Oracle Stored Procedure

The following code is for those who need to receive e-mails from the database to report a correct ending backup, a corrupted backup, a database fail, or any message database/admin related.
The code is based on the UTL_SMTP package supplied in Oracle8i V8.1.6 (release 2).

First step:

Installing necessary JAVA classes, they are installed with the typical installation but if you chose custom, perform the following commands:

 	Connect as SYS User. Run %ORACLE_HOME/javavm/plsql/jlib/initjvm.sql Run %ORACLE_HOME/rdbms/java/install/initplsj.sql 

Second Step:

Write the following PL/SQL Code:

   CREATE OR REPLACE PROCEDURE send_mail  (  sender     IN VARCHAR2,   recipient  IN VARCHAR2,   subject    IN VARCHAR2,   message    IN VARCHAR2)       IS   mailhost     VARCHAR2(30) := 'YOUR.SMTP.SERVER';   mail_conn    utl_smtp.connection;      crlf VARCHAR2( 2 ):= CHR( 13 ) || CHR( 10 );   mesg VARCHAR2( 1000 );		           BEGIN		 		  mail_conn := utl_smtp.open_connection(mailhost, 25); 	          mesg:= 'Date: ' || TO_CHAR( SYSDATE, 'dd Mon yy hh24:mi:ss' ) || crlf ||	         'From:  <'||sender||'>' || crlf ||        	 'Subject: '||subject || crlf ||	         'To: '||recipient || crlf ||        	 '' || crlf || message;		  utl_smtp.helo(mail_conn, mailhost);		  utl_smtp.mail(mail_conn, sender);		  utl_smtp.rcpt(mail_conn, recipient);, mesg);		  utl_smtp.quit(mail_conn);				END;

And ready to use, try it from sqlplus:

 begin send_mail('sender e-mail','dest e-mail','here goes subject','here goes message');end;/

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS