Send Email from Oracle Procedure

How to send Email via PL/SQL Procedure?

Questions by p_pramanik123

Showing Answers 1 - 1 of 1 Answers

rem -----------------------------------------------------------------------
rem Filename:   smtp.sql
rem Purpose:    Send e-mail messages from PL/SQL
rem Notes:      From Oracle8i release 8.1.6 one can send e-mail messages
rem  directly from PL/SQL using either the UTL_TCP or UTL_SMTP
rem  packages. JServer needs to be installed and configured.
rem             Pont the IP Address to your local SMTP (Simple Mail
rem             Transport) Server. No pipes or external procedures are
rem             required.
rem Date:       24-nov-2008
rem Author:     Rajiv Gupta
rem -----------------------------------------------------------------------

CREATE OR REPLACE PROCEDURE SEND_MAIL (
  msg_from    varchar2 := 'oracle',
  msg_to      varchar2,
  msg_subject varchar2 := 'E-Mail message from RAJIV',
  msg_text    varchar2 := '' )
IS
  c  utl_tcp.connection;
  rc integer;
BEGIN
  c := utl_tcp.open_connection('127.0.0.1', 25);       -- open the SMTP port 25 on local machine
  dbms_output.put_line(utl_tcp.get_line(c, TRUE));
  rc := utl_tcp.write_line(c, 'HELO localhost');
  dbms_output.put_line(utl_tcp.get_line(c, TRUE));
  rc := utl_tcp.write_line(c, 'MAIL FROM: '||msg_from);
  dbms_output.put_line(utl_tcp.get_line(c, TRUE));
  rc := utl_tcp.write_line(c, 'RCPT TO: '||msg_to);
  dbms_output.put_line(utl_tcp.get_line(c, TRUE));
  rc := utl_tcp.write_line(c, 'DATA');                 -- Start message body
  dbms_output.put_line(utl_tcp.get_line(c, TRUE));
  rc := utl_tcp.write_line(c, 'Subject: '||msg_subject);
  rc := utl_tcp.write_line(c, '');
  rc := utl_tcp.write_line(c, msg_text);
  rc := utl_tcp.write_line(c, '.');                    -- End of message body
  dbms_output.put_line(utl_tcp.get_line(c, TRUE));
  rc := utl_tcp.write_line(c, 'QUIT');
  dbms_output.put_line(utl_tcp.get_line(c, TRUE));
  utl_tcp.close_connection(c);                         -- Close the connection
EXCEPTION
  when others then
       raise_application_error(
           -20000, 'Unable to send e-mail message from pl/sql because of: '||
           sqlerrm);
END;
/
show errors

--  Examples:
set serveroutput on

exec send_mail(msg_to  =>'rajivgupta780184@gmail.com');

exec send_mail(msg_to  =>'rajivgupta780184@gmail.com',  -
        msg_text=>'Look I can send mail from plsql' -
              );


Thanks & Regards
RAJIV GUPTA
Yogik Technologies pvt. ltd.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions