How Can I Send Emails in PL/SQL (Oracle)?

橙三吉。 提交于 2021-01-29 06:44:06

问题


I have a procedure which count the number of rows in SMS_OUTBOX table and send emails if its row cont is over 1000. My procedure is given below:

CREATE OR REPLACE PROCEDURE SEND_EMAIL_ABOUT_PENDING_SMS IS
  CHECK_SMS_COUNT NUMBER := 1000;
  CURRENT_SMS_COUNT NUMBER;
BEGIN
  SELECT COUNT(1) INTO CURRENT_SMS_COUNT FROM SMS_SCHEMA.SMS_OUTBOX;

  IF CURRENT_SMS_COUNT >= CHECK_SMS_COUNT THEN
    UTL_MAIL.SEND(
        sender=>'<SENDER_EMAIL>',
        recipients=>'<RECIPIENT_EMAIL>',
        subject=>'Pending SMS',
        Message=>'Pending SMS count exceeded.'
    );
  END IF;
END SEND_EMAIL_ABOUT_PENDING_SMS;
/

When I compile the above I got this error.

Then I tried this line of code to execute without the procedure:

EXEC UTL_MAIL.SEND(
    sender=>'<SENDER_EMAIL>',
    recipients=>'<RECIPIENT_EMAIL>',
    subject=>'Pending SMS',
    Message=>'Pending SMS count exceeded.'
);

Then I got this error:

PLS-00302: component 'SEND' must be declared

I am very new to Oracle (and PL/SQL). Can anyone please help me on sending emails in Oracle and what are the configurations needed?


回答1:


Please check whether UTL_MAIL has been installed properly.

Follow the link UTL_MAIL for information and configuration required

Setup
The package is loaded by running the following scripts.

CONN sys/password AS SYSDBA
@$ORACLE_HOME/rdbms/admin/utlmail.sql
@$ORACLE_HOME/rdbms/admin/prvtmail.plb

In addition the SMTP_OUT_SERVER parameter must be set to identify the SMTP server.

CONN sys/password AS SYSDBA
ALTER SYSTEM SET smtp_out_server='smtp.domain.com' SCOPE=SPFILE;

-- Instance restart only necessary in 10gR1.
SHUTDOWN IMMEDIATE
STARTUP



回答2:


Alternatively, you can use utl_http.request method as

  v_url := 'http://<yourWebSite>/notification.asp?sender=<SENDER_EMAIL>
                                                 &recipients=<RECIPIENT_EMAIL>
                                                 &subject=Pending SMS
                                                 &message=Pending SMS count exceeded.';

  v_rep := utl_http.request(utl_url.escape(v_url, false, 'UTF-8'));

where

  v_url varchar2(4000);
  v_rep varchar2(4000);

need to be declared before begin keyword.



来源:https://stackoverflow.com/questions/54968189/how-can-i-send-emails-in-pl-sql-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!