Send mail from oracle store procedure ,Oracle 11g - sql

I am trying to send mail using utl_mail and for smtp server smtp.gmail.com with port 25 or 587.
If i use port 25,getting error, must use STARTTLS command first and for 587 its going in an infinite loop.
my code snippet are below
create or replace
PROCEDURE TEST_UTL_MAIL AS
BEGIN
utl_mail.send(
sender => 'xxx#gmail.com',
recipients => 'xxx#gmail.com',
subject => 'Testing utl_mail',
message => 'The receipt of this email means'
);
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,'The following error has occured: ' || sqlerrm);
END;
Any help will be much appreciated.

You may have access control list issues. What is the error the code is reporting?
Also can you supply the output of this query (you'll need to supply the user parameter):
SELECT host, lower_port, upper_port, acl,
DECODE(
DBMS_NETWORK_ACL_ADMIN.CHECK_PRIVILEGE_ACLID(aclid, '&USER', 'connect'),
1, 'GRANTED', 0, 'DENIED', null) privilege
FROM dba_network_acls
;

Related

Using a DBMS_PIPE.PACK_MESSAGE and DBMS_PIPE.SEND_MESSAGE in a trigger

Env: Oracle 12c
I'm looking at using Oracle DBMS_PIPE within a table trigger that will be used by many users. The trigger will fire only on a STATUS update as per below:
CREATE OR REPLACE TRIGGER MY_TRG
AFTER UPDATE OF STATUS ON "MY_TABLE"
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
declare
v_status INTEGER;
begin
if :OLD.status = 'ERROR' and (:NEW.status = 'OK' or :NEW.status = 'ERROR') then
DBMS_PIPE.PACK_MESSAGE(:OLD.id_key);
DBMS_PIPE.PACK_MESSAGE(:NEW.status);
v_status := DBMS_PIPE.SEND_MESSAGE('MY_PIPE');
if v_status != 0 THEN
raise_application_error(num => -20002,msg => 'error message on trigger!');
end if;
end if;
end;
The following call will be initiated from an Oracle APEX page process where this can be submitted again by multiple users.
DBMS_PIPE.receive_message(pipename => 'MY_PIPE', timeout => 10);
My question is, for each user here, do I need to ensure that the PIPE NAME is specific to each user, so that they only see their messages within their PIPE or can just the one 'MY_PIPE' pipe name handle all transactions for multiple users?
If in the case that each user needs their own designated PIPE NAME, how would I do this, if the SEND_MESSAGE('USER_1_PIPE') is triggered from a table trigger, which my receive_message_proc will be unaware of this 'USER_1_PIPE' name.
My create pipe is like this:
v_res := DBMS_PIPE.create_pipe(pipename => 'MY_PIPE', private => TRUE);
I assume I need to tag each user with their own private pipe name - is this correct?
Private pipes are private the username that created them. If you have multiple people logging on with the same user account, then they are all going to be able to see that pipe.
But perhaps a larger issue is that pipe are not transactional. So the moment that trigger fires, the message get put in the pipe...even if that transaction later rolls back, or fails or anything else that does not finally update the status. Moreover, the pipe message will be sent BEFORE the transaction commits. Another session (receiving that pipe message) will not be able to see changes done until the commit occurs, which can lead to timing inconsistencies.
Perhaps AQ (Advanced Queuing) is an alternative you might want to consider. Messages on a queue by default are transactional, so the message on the queue is then nicely bound to whether your changes to STATUS actually succeed.
The calling application just listens on a queue rather than on a pipe message.

ORACLE APEX ADMIN LOGIN ISSUE

I am getting an authentication required message on google chrome when logging into oracle apex admin.
screenshot of error:
I'm assuming you're using the embedded PL/SQL Gateway and the XDB Protocol Server, correct? If so, please ensure:
You have run #apex_epg_config.sql
You have unlocked the ANONYMOUS database user
https://docs.oracle.com/database/apex-5.1/HTMIG/configuring-embedded-PL-SQL-gateway.htm#HTMIG29205
Why this happens? Because the ANONYMOUS account is locked and/or expired.
Log in to main container database (not PDB)
Run the following query to determine status:
select account_status from dba_users where username = 'ANONYMOUS';
If account_status is expired, run the following:
alter user ANONYMOUS identified by anonymous;
alter user ANONYMOUS account unlock;
If account_status is locked, run the following:
alter user ANONYMOUS account unlock;
When using IE instead of Chrome the login message refers to XDB. It seems like a problem with anonymous connection. This thread gives a solution for enabling this anonymous access. Though I think this is not probably a good solution, I don't have enough knowledge to solve it in a better way. In case the link is broken, I'm pasting the solution here. It worked for me, and I've seen this solution in a few more places so I maybe this is quite common...
(with CONN sys/password AS SYSDBA)
SET SERVEROUTPUT ON
DECLARE
l_configxml XMLTYPE;
l_value VARCHAR2(5) := 'true'; -- (true/false)
BEGIN
l_configxml := DBMS_XDB.cfg_get();
IF l_configxml.existsNode('/xdbconfig/sysconfig/protocolconfig/httpconfig/allow-repository-anonymous-access') = 0 THEN
-- Add missing element.
SELECT insertChildXML
(
l_configxml,
'/xdbconfig/sysconfig/protocolconfig/httpconfig',
'allow-repository-anonymous-access',
XMLType('<allow-repository-anonymous-access xmlns="http://xmlns.oracle.com/xdb/xdbconfig.xsd">' ||
l_value ||
'</allow-repository-anonymous-access>'),
'xmlns="http://xmlns.oracle.com/xdb/xdbconfig.xsd"'
)
INTO l_configxml
FROM dual;
DBMS_OUTPUT.put_line('Element inserted.');
ELSE
-- Update existing element.
SELECT updateXML
(
DBMS_XDB.cfg_get(),
'/xdbconfig/sysconfig/protocolconfig/httpconfig/allow-repository-anonymous-access/text()',
l_value,
'xmlns="http://xmlns.oracle.com/xdb/xdbconfig.xsd"'
)
INTO l_configxml
FROM dual;
DBMS_OUTPUT.put_line('Element updated.');
END IF;
DBMS_XDB.cfg_update(l_configxml);
DBMS_XDB.cfg_refresh;
END;
/

Getting TNS :Connect timeout occurred in Oracle

I have the below function and I am quite frequently getting the below errors when front end ( ADF ) calls this function to get count.
Different errors comes at different times.
I think sometimes the listener is down so it might occur but other errors ?It happens when user from front end click on the link and that link in turn calls this database function.
I'm not able to find the issue in the function :-(
1) error "-12170 : ORA-12170: TNS:Connect timeout occurred"
2) error "-12571 : ORA-12571: TNS:packet writer failure"
3) error "-12541 : ORA-12541: TNS:no listener
4) error "-12514 : ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
5) error "-3113 : ORA-03113: end-of-file on communication channel
ORA-02063: preceding line from DBLINK"
6) error "-2051 : ORA-02051: another session or branch in same transaction failed or finalized
ORA-02063: preceding line from DBLINK"
FUNCTION GET_A (p_in IN VARCHAR2) RETURN NUMBER
AS
PRAGMA AUTONOMOUS_TRANSACTION;
PRE_COUNT NUMBER := NULL;
BEGIN
SELECT GET_B#DBLINK (p_in,NULL)INTO PRE_COUNT FROM dual;
COMMIT;
-- dbms_lock.sleep(2);
DBMS_SESSION.CLOSE_DATABASE_LINK('DBLINK');
COMMIT;
RETURN PRE_COUNT;
EXCEPTION
WHEN OTHERS
THEN
ROLLBACK;
LOG_ERR ( p_cd => SQLCODE,p_msg => SQLERRM);
RETURN -1;
END GET_A;

Oracle PL/SQL debug procedure - using $$plsql_line

I'm looking to create a procedure that helps with debugging specific variables, and for the sake of re-usability I'd like to store it in it's own procedure and pass in the specific variable when needed such as
debugz(var_x);
and the debug procedure would do the following -
PROCEDURE debugz (var_x VARCHAR2(1000))
AS
BEGIN
DBMS_OUTPUT.put_line ('Variable value: '|| var_x || ' | Line number: ' || $$plsql_line ||' | Unit: '|| $$plsql_unit);
END;
The problem is, I want the PL/SQL line number and PL/SQL unit to be based off of where the procedure call originates, i.e. the line/unit of "debugz(var_x);". Is there anyway for debugz to output that line number without passing in additional information?
Check out DBMS_UTILITY.FORMAT_CALL_STACK
You may need/want to parse the output depending on your exact requirements, but may be a good place to start.
Oracle 10g onwards.
Also note DBMS_UTILITY.FORMAT_ERROR_BACKTRACE and DBMS_UTILITY.FORMAT_ERROR_STACK for error handling.
There is a very good debugging package unit called debugf which provides good functionality
The debug file contains information such as session id,date and time,packages being called and the line number of each debug message and the debug message itself
Example usage is given below
This is used to intialize the debug, the first parameter 'ALL' meaning all modules(can be function,procedure or package etc) and SYSTEM meaning the schema i want to debug
debug.init(p_modules => 'ALL',p_file =>'C:\debugf123\temp\test.txt',p_user =>'SYSTEM',p_show_date => 'YES',p_date_format =>'DD/MM/YYYY HH24:MI:SS',p_name_len => 30,p_show_sesid => 'YES');
This one works like printf in C and the maximum you can give is 10 parameters where v_word1 is a parameter
debug.f('the first is %s',v_word1);
This is same as debug.f but here you can give more than 10 parameters
debug.fa('the third is %s and %s',debug.argv(v_word1,v_amount));
The source code for this package is available at
http://gerardnico.com/wiki/database/oracle/debugf
OWA_UTIL.WHO_CALLED_ME(
owner OUT VARCHAR2,
name OUT VARCHAR2,
lineno OUT NUMBER,
caller_t OUT VARCHAR2);
Have you tried remote debugging? Here's how you can do it in SQL Developer:
1) Reference:
http://sueharper.blogspot.ca/2006/07/remote-debugging-with-sql-developer_13.html
2) User privileges:
grant EXECUTE on DBMS_DEBUG_JDWP to USERXX;
grant DEBUG CONNECT SESSION to USERXX;
grant DEBUG ANY PROCEDURE to USERXX;
3) Set Remote Debug on USERXX connection in SQL Developer:
Port: 80 (use 4000 if not blocked by firewall)
Local Address: IP address of your local machine
4) Compile code (package, procedure or function) in USERXX in SQL Developer for debug
5) Set breakpoints in code
6) On the remote (this could be your application invoking the PLSQL code):
before invoking the PLSQL code run/include the following:
DBMS_DEBUG_JDWP.CONNECT_TCP( 'IP address in 3', port in 3 ) when this is run SQL developer will switch to debug mode.
7) Run application or invoke procedure from remote as USERXX
8) SQL developer stops at first breakpoint, step into, view/change values and etc.

sql db mail problem while sending in bulk

I am using db mail(sql server 2005) to send bulk email(>2000).
The code tat i use is,
exec msdb..sp_send_dbmail
#profile_name = 'My Profile',
#recipients = 'raghav.cinch#gmail.com',
#subject = 'test',
#body = 'test',
#body_format = 'HTML'
If i send few emails(less than 100), all emails are sent successfully. But only bulk emails give me error.
The error I get is,
The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 8 (2011-09-27T21:29:17). Exception Message: Cannot send mails to mail server. (Unable to send to all recipients.).
)
The error comes after 100 or 105 mails. The email addresses are correct and if i sent in cycles of 100, all mails are sent successfully.
I believe it should be some configuration settings tat need to be tweaked. Could someone pls help me in fixing it..
Thanks in advance.
Changed the iis6 settings, max queue length for the smtp server and this worked like charm.