ACL error when trying to send mail via Oracle UTL_SMTP
NickName:João Abrantes Ask DateTime:2018-12-18T22:32:34

ACL error when trying to send mail via Oracle UTL_SMTP

I was trying to send an email via oracle utl_smtp, but every time i execute the apex_mail_p.mail procedure i get an ACL error, to be more precise it's the error ORA-24247. But i've created the acl, added the right previliege and also added the host and port to the ACL. I don't understand why it's giving me an error.

This is the code:

    -- create acl
begin
        dbms_network_acl_admin.create_acl (
                acl             => 'gmail.xml',
                description     => 'Normal Access',
                principal       => 'CONNECT',
                is_grant        => TRUE,
                privilege       => 'connect',
                start_date      => null,
                end_date        => null
        );
end;
/

-- add priviliege to acl
begin
  dbms_network_acl_admin.add_privilege ( 
  acl       => 'gmail.xml',
  principal    => 'MY_PROJECT',
  is_grant    => TRUE, 
  privilege    => 'connect', 
  start_date    => null, 
  end_date    => null); 
end;
/
-- assign host, port to acl
begin
  dbms_network_acl_admin.assign_acl (
  acl => 'gmail.xml',
  host => 'localhost',
  lower_port => 25,
  upper_port => 25);
end;
/



create or replace package apex_mail_p
is
   g_smtp_host      varchar2 (256)     := 'localhost';
   g_smtp_port      pls_integer        := 25;
   g_smtp_domain    varchar2 (256)     := 'gmail.com';
   g_mailer_id constant varchar2 (256) := 'Mailer by Oracle UTL_SMTP';
   -- send mail using UTL_SMTP
   procedure mail (
      p_sender in varchar2
    , p_recipient in varchar2
    , p_subject in varchar2
    , p_message in varchar2
   );
end;
/
create or replace package body apex_mail_p
is
   -- Write a MIME header
   procedure write_mime_header (
      p_conn in out nocopy utl_smtp.connection
    , p_name in varchar2
    , p_value in varchar2
   )
   is
   begin
      utl_smtp.write_data ( p_conn
                          , p_name || ': ' || p_value || utl_tcp.crlf
      );
   end;
   procedure mail (
      p_sender in varchar2
    , p_recipient in varchar2
    , p_subject in varchar2
    , p_message in varchar2
   )
   is
      l_conn           utl_smtp.connection;
      nls_charset    varchar2(255);
   begin
      -- get characterset
      select value
      into   nls_charset
      from   nls_database_parameters
      where  parameter = 'NLS_CHARACTERSET';
      -- establish connection and autheticate
      l_conn   := utl_smtp.open_connection (g_smtp_host, g_smtp_port);
      utl_smtp.ehlo(l_conn, g_smtp_domain);  
      utl_smtp.command(l_conn, 'auth login');
      utl_smtp.command(l_conn,utl_encode.text_encode('[email protected]', nls_charset, 1));
      utl_smtp.command(l_conn, utl_encode.text_encode('mypassword123', nls_charset, 1));
      -- set from/recipient
      utl_smtp.command(l_conn, 'MAIL FROM: <'||p_sender||'>');
      utl_smtp.command(l_conn, 'RCPT TO: <'||p_recipient||'>');
      -- write mime headers
      utl_smtp.open_data (l_conn);
      write_mime_header (l_conn, 'From', p_sender);
      write_mime_header (l_conn, 'To', p_recipient);
      write_mime_header (l_conn, 'Subject', p_subject);
      write_mime_header (l_conn, 'Content-Type', 'text/plain');
      write_mime_header (l_conn, 'X-Mailer', g_mailer_id);
      utl_smtp.write_data (l_conn, utl_tcp.crlf);
      -- write message body
      utl_smtp.write_data (l_conn, p_message);
      utl_smtp.close_data (l_conn);
      -- end connection
      utl_smtp.quit (l_conn);
   exception
      when others
      then
         begin
           utl_smtp.quit(l_conn);
         exception
           when others then
             null;
         end;
         raise_application_error(-20000,'Failed to send mail due to the following error: ' || sqlerrm);   
   end;
end;
/



begin
   apex_mail_p.mail('[email protected]', '[email protected]', 'Test', 'Its only a test');
end;
/

Copyright Notice:Content Author:「João Abrantes」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53835269/acl-error-when-trying-to-send-mail-via-oracle-utl-smtp

Answers
Barbaros Özhan 2018-12-18T14:52:36

\nReplace principal arguments with your database user names,\nAdd resolve privilege also,\nUsing redundant begin..end block is unnecessary.\n\n\nSo you may consider using the one below :\n\n -- create acl\n begin\n dbms_network_acl_admin.create_acl (\n acl => 'gmail.xml',\n description => 'Normal Access',\n principal => 'DB_USER',\n is_grant => TRUE,\n privilege => 'connect',\n start_date => null,\n end_date => null);\n\n -- add priviliege to acl\n dbms_network_acl_admin.add_privilege ( \n acl => 'gmail.xml',\n principal => 'DB_USER',\n is_grant => TRUE, \n privilege => 'connect', \n start_date => null, \n end_date => null); \n\n dbms_network_acl_admin.add_privilege ( \n acl => 'gmail.xml',\n principal => 'DB_USER',\n is_grant => TRUE, \n privilege => 'resolve', \n start_date => null, \n end_date => null); \n\n -- assign host, port to acl\n dbms_network_acl_admin.assign_acl (\n acl => 'gmail.xml',\n host => 'localhost',\n lower_port => 25,\n upper_port => 25);\n end;\n /\n",


More about “ACL error when trying to send mail via Oracle UTL_SMTP” related questions

ACL error when trying to send mail via Oracle UTL_SMTP

I was trying to send an email via oracle utl_smtp, but every time i execute the apex_mail_p.mail procedure i get an ACL error, to be more precise it's the error ORA-24247. But i've created the acl,...

Show Detail

APEX_MAIL.SEND function not working though its not giving any error

Have to send email from oracle apex using APEX_MAIL.SEND() method. I am using the code: BEGIN apex_mail.send(p_to =&gt; '[email protected]'/*l_to_addr*/, p_from =...

Show Detail

Debugging with Oracle's utl_smtp

A client of mine uses Oracle 9i's utl_smtp to send mails out notifications to managers when their employees have made travel requests and they woul like quite a few changes made to the mailouts don...

Show Detail

Oracle 11G XE - ORA-46105 error while creating an ACL

I have created a database manually on a windows 2008 R2 server - 64 bit having Oracle 11G - Express Edition installed on it.I have also installed XDB on it manually. I have validated the XDB insta...

Show Detail

How to re-design code with UTL_SMTP - Oracle Apex send mail

It's been a hard time not able to make my code to work with UTL_SMTP package. Can some one guide me to change below code and make it work with UTL_SMTP method I created a mailing application which ...

Show Detail

cannot send emails with Oracle 11g APEX5

Trying to resolve an issue with sending emails using Oracle APEX5 11g. I've set up the administration settings as per Oracle instructions: SMTP Host Address - Defines the server address of the SMTP

Show Detail

UTL_SMTP in postgresql

Im curruntly migrating oracle schema to postgresql 9.5 . Im using Ora2pg and it converted for me one function which is reponsible for sending mail to pgplsql. My code : CREATE OR REPLACE FUNCTION

Show Detail

ACL for Sending e-Mail with APEX Oracle 11.2

I'm trying to send e-Mail as a simple Send e-Mail process with Oracle APEX 11.2, and I can't figure out how the correct ACL has to be set. I tried this, but no success, still getting: ORA-24247: ne...

Show Detail

Sending multiple emails using UTL_SMTP in oracle

I am trying to write a procedure which send emails from Oracle DB using UTL_SMTP functions. Its working fine if I am only sending one email address but if I am passing multiple emails as comma or

Show Detail

ORA-24247: network access denied by access control list (ACL) while sending email oracle

i have done all the activity i.e mention below, please tell which step / activity i am missing. BEGIN DBMS_NETWORK_ACL_ADMIN.CREATE_ACL( acl =&gt; '

Show Detail