Sunday, February 27, 2011

Send mail from oracle db

CREATE OR REPLACE PROCEDURE SEND_MAIL (
mesg_to varchar2,
mesg_subject varchar2,
mesg_text varchar2 )
IS
c utl_smtp.connection;
rc integer;
mesg_from varchar2(50) := 'MYDEVDB';
mailhost VARCHAR2(30) := 'name of your mailhost';

BEGIN
c := utl_smtp.open_connection(mailhost, 25); -- SMTP on port 25
utl_smtp.helo(c, mailhost);
utl_smtp.mail(c, msg_from);
utl_smtp.rcpt(c, msg_to);

utl_smtp.data(c,'From: MYDEVDB Oracle Database' || utl_tcp.crlf ||
'To: ' || msg_to || utl_tcp.crlf ||
'Subject: ' || msg_subject ||
utl_tcp.crlf || msg_text);
utl_smtp.quit(c);

EXCEPTION
WHEN UTL_SMTP.INVALID_OPERATION THEN
dbms_output.put_line(' Invalid Operation in Mail attempt
using UTL_SMTP.');
WHEN UTL_SMTP.TRANSIENT_ERROR THEN
dbms_output.put_line(' Temporary e-mail issue - try again');
WHEN UTL_SMTP.PERMANENT_ERROR THEN
dbms_output.put_line(' Permanent Error Encountered.');
END;
-------------------------------------------
Guideline taken from internet.