Wednesday, February 3, 2010

Procedure To Send Email from oracle database

CREATE OR REPLACE PROCEDURE EEMAIL (p_smtp_hostname varchar2,p_smtp_portnum varchar2,p_from varchar2,p_recpt_1 varchar2,p_recpt_2 varchar2,subject VARCHAR2,MSG VARCHAR2)
IS
v_connection UTL_SMTP.CONNECTION;
v_header varchar2(1000);
crlf VARCHAR2(2):= UTL_TCP.CRLF;
--subject VARCHAR2(1000);
BEGIN
v_connection := utl_smtp.open_connection(p_smtp_hostname,p_smtp_portnum);
utl_smtp.helo(v_connection,p_smtp_hostname);
utl_smtp.mail(v_connection,p_from);
utl_smtp.rcpt(v_connection,p_recpt_1);
utl_smtp.rcpt(v_connection,p_recpt_2);
--utl_smtp.rcpt(v_connection,p_recpt_3);
v_header := 'Date: 'TO_CHAR(SYSDATE,'dd Mon yy hh24:mi:ss')crlf
'From: 'p_from''crlf
'Subject: 'subjectcrlf
'To: 'p_recpt_1crlf
'CC: 'p_recpt_2';'crlf;
-- 'CC: 'p_recpt_3crlf;
utl_smtp.open_data(v_connection);
utl_smtp.write_data(v_connection,v_header);
-- RFC 821 Format (SMTP) blank line between message header and message body
utl_smtp.write_data(v_connection,crlf);
utl_smtp.write_data(v_connection,MSGcrlfcrlf' ');

utl_smtp.close_data(v_connection);
utl_smtp.quit(v_connection);

/*EXCEPTION
WHEN UTL_SMTP.INVALID_OPERATION THEN
INSERT INTO ERROR_UPLOAD_SQL_MSG (SQL_CODE,SQL_ERROR_MSG,ERROR_DATE)
VALUES ('UTL_SMTP.INVALID_OPERATION','Invalid Operation in Mail attempt using UTL_SMTP.',SYSDATE);
COMMIT;
WHEN UTL_SMTP.TRANSIENT_ERROR THEN
INSERT INTO ERROR_UPLOAD_SQL_MSG (SQL_CODE,SQL_ERROR_MSG,ERROR_DATE)
VALUES ('UTL_SMTP.TRANSIENT_ERROR','Temporary e-mail issue - try again.',SYSDATE);
COMMIT;
WHEN UTL_SMTP.PERMANENT_ERROR THEN
INSERT INTO ERROR_UPLOAD_SQL_MSG (SQL_CODE,SQL_ERROR_MSG,ERROR_DATE)
VALUES ('UTL_SMTP.PERMANENT_ERROR','Permanent Error Encountered.',SYSDATE);
COMMIT;*/

-- END EMAIL_ERRORS;
END;
/

No comments:

Post a Comment