March 7, 2014

For scheduling Mail from Oracle database.

Here is one example for configuring mail that running on scheduler. 
The mail will send when some one disable the job.  
* For security reasons, UTL_MAIL is not enabled by default. You must install it by connecting to SYS, then executing the " utlmail.sql " and " prvtmail.plb " scripts in the $ORACLE_HOME/rdbms/admin directory.


create or replace procedure proc1
is
i number;
begin

select max(id) into i from tab1;
if i is null then

i:=0;
insert into tab1 values(i);
else
i:=i+1;
insert into tab1 values(i);
end if;

commit;

end;
/


BEGIN
  DBMS_SCHEDULER.create_job (
    job_name        => 'tab1_ins_job',
    job_type        => 'STORED_PROCEDURE',
    job_action      => 'PROC1',
    start_date      => SYSDATE,
    repeat_interval => 'freq=minutely; interval=1',
    enabled         => TRUE,
    comments        => 'Job is used for testing enable and disable by the app');
END;
/

create or replace procedure manage_proc1(mod1 in number)
is
begin
if mod1 = 1 then
DBMS_SCHEDULER.disable('tab1_ins_job');
EXECUTE IMMEDIATE 'ALTER SESSION SET smtp_out_server = ''192.168.5.222:25''';
  UTL_MAIL.send(sender => 'me@address.com',recipients => 'mail_id@mymail.com',subject => 'Test Mail Job Disabled ',message => 'Job disabled send_mail',mime_type =>'text; charset=us-ascii');
else
DBMS_SCHEDULER.enable('tab1_ins_job');
EXECUTE IMMEDIATE 'ALTER SESSION SET smtp_out_server = ''192.168.5.222:25''';
  UTL_MAIL.send(sender => 'me@address.com',recipients => 'mail_id@mymail.com',subject => 'Test Mail Job Enabled ',message => 'Job Enabled send_mail',mime_type =>'text; charset=us-ascii');
end if;
end;
/


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- In order to successful execute UTL_MAIL package we need to create Access control list. Please follow the example shown below.

Oracle 11g introduces fine grained access to network services using access control lists (ACL) in the XML DB repository, allowing control over which users access which network resources, regardless of package grants.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

BEGIN
   DBMS_NETWORK_ACL_ADMIN.create_acl (
      acl           => 'test_acl_file.xml',
      description   => 'A test of the ACL functionality',
      principal     => 'SCOTT', -- principal is the username who execute the mail package
      is_grant      => TRUE,
      privilege     => 'connect',
      start_date    => SYSTIMESTAMP,
      end_date      => NULL);
   COMMIT;
END;
/

BEGIN
   DBMS_NETWORK_ACL_ADMIN.assign_acl (acl          => 'test_acl_file.xml',
                                      HOST         => '192.168.5.222', -- Mail server IP, we can give mail server domain address
                                      lower_port   => 25,      -- And its port
                                      upper_port   => NULL);
   COMMIT;
END;
/

No comments: