zoukankan      html  css  js  c++  java
  • Oracle管理监控之使用utl_mail自动邮件报警配置

    --代发邮件存储过程源码如下:

    CREATE OR REPLACE PROCEDURE send_mail(p_recipient VARCHAR2, -- 邮件接收人
                                          p_subject   VARCHAR2, -- 邮件标题
                                          p_message   VARCHAR2 -- 邮件正文
                                          ) IS

      --下面四个变量请根据实际邮件服务器进行赋值
      v_mailhost VARCHAR2(30) := 'smtp.szlongtu.com'; --SMTP服务器地址
      v_user     VARCHAR2(30) := 'wcwen@qq.com'; --登录SMTP服务器的用户名
      v_pass     VARCHAR2(20) := 'XXXXXX'; --登录SMTP服务器的密码
      v_sender   VARCHAR2(50) := 'wcwen@qq.com'; --发送者邮箱,一般与 ps_user 对应

      v_conn UTL_SMTP. connection; --到邮件服务器的连接
      v_msg  varchar2(4000); --邮件内容

    BEGIN

      v_conn := UTL_SMTP.open_connection(v_mailhost, 25);
      UTL_SMTP.ehlo(v_conn, v_mailhost); --是用 ehlo() 而不是 helo() 函数
      --否则会报:ORA-29279: SMTP 永久性错误: 503 5.5.2 Send hello first.

      UTL_SMTP.command(v_conn, 'AUTH LOGIN'); -- smtp服务器登录校验
      UTL_SMTP.command(v_conn,
                       UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(v_user))));
      UTL_SMTP.command(v_conn,
                       UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(v_pass))));
      --  dbms_output.put_line('...........................');

      UTL_SMTP.mail(v_conn, '<' || v_sender || '>'); --设置发件人

      UTL_SMTP.rcpt(v_conn, '<' || p_recipient || '>'); --设置收件人

      -- 创建要发送的邮件内容 注意报头信息和邮件正文之间要空一行
      v_msg := 'Date:' || TO_CHAR(SYSDATE, 'dd mon yy hh24:mi:ss') ||
               UTL_TCP.CRLF || 'From: ' || '<' || v_sender || '>' ||
               UTL_TCP.CRLF || 'To: ' || '<' || p_recipient || '>' ||
               UTL_TCP.CRLF || 'Subject: ' || p_subject || UTL_TCP.CRLF ||
               UTL_TCP.CRLF -- 这前面是报头信息
               || p_message; -- 这个是邮件正文
      dbms_output.put_line(v_msg);
      UTL_SMTP.open_data(v_conn); --打开流
      UTL_SMTP.write_raw_data(v_conn, UTL_RAW.cast_to_raw(v_msg)); --这样写标题和内容都能用中文
      UTL_SMTP.close_data(v_conn); --关闭流
      UTL_SMTP.quit(v_conn); --关闭连接

    EXCEPTION

      WHEN OTHERS THEN
        DBMS_OUTPUT.put_line(DBMS_UTILITY.format_error_stack);
        DBMS_OUTPUT.put_line(DBMS_UTILITY.format_call_stack);
     
    END send_mail;

    --配置acl

    select * from dba_network_acls;

    >创建acl

    BEGIN
      DBMS_NETWORK_ACL_ADMIN.create_acl(acl         => 'acl_sendmail_for_icme.xml',
                                        description => 'send mail of the ACL functionality',
                                        principal   => 'SCOTT',
                                        is_grant    => TRUE,
                                        privilege   => 'connect',
                                        start_date  => null,
                                        end_date    => NULL);

      COMMIT;
    END;
    /

    >SELECT any_path FROM resource_view WHERE any_path like '/sys/acls/%.xml';

    >添加主机或域名

    BEGIN
      DBMS_NETWORK_ACL_ADMIN.assign_acl(acl        => 'acl_sendmail_for_icme.xml',
                                        host       => 'smtp.szlongtu.com',
                                        lower_port => 25,
                                        upper_port => null);
    END;
    /


    DECLARE
      v_mailsever_host VARCHAR2(30) := 'smtp.szlongtu.com';
      v_mailsever_port PLS_INTEGER := 25;
      l_mail_conn      UTL_SMTP.CONNECTION;
    BEGIN
      l_mail_conn := UTL_SMTP.OPEN_CONNECTION(v_mailsever_host,
                                              v_mailsever_port);
    END;
    /

    select DBMS_NETWORK_ACL_ADMIN.CHECK_PRIVILEGE('acl_sendmail_for_icme.xml','SCOTT','connect') from dual;

    grant execute on send_mail to scott;

    select DBMS_NETWORK_ACL_ADMIN.CHECK_PRIVILEGE('acl_sendmail_for_icme.xml','SYS','connect') from dual;

    >授权用户

    BEGIN
      dbms_network_acl_admin.add_privilege(acl        => 'acl_sendmail_for_icme.xml',
                                           principal  => 'SYS',
                                           is_grant   => TRUE,
                                           privilege  => 'connect',
                                           start_date => SYSTIMESTAMP,
                                           end_date   => NULL);
      COMMIT;
    END;
    /

    SELECT acl,
           principal,
           privilege,
           is_grant,
           TO_CHAR(start_date, 'DD-MON-YYYY') AS start_date,
           TO_CHAR(end_date, 'DD-MON-YYYY') AS end_date
      FROM dba_network_acl_privileges;

    --清空acl信息

    BEGIN
      DBMS_NETWORK_ACL_ADMIN.drop_acl(acl => 'acl_sendmail_for_icme.xml');
    end;

  • 相关阅读:
    ecshop简单结构
    关于dwt文件和lbi文件
    ecshop各个文件夹作用
    修改ecshop模板体会
    PHP的字符串函数
    数组的相关处理函数
    PHP连接数据库
    Headless谷歌selenium使用
    关于无界面浏览器、request模块、tesseract模块的部分笔记
    爬取某城市公交钱路--xpath过滤
  • 原文地址:https://www.cnblogs.com/wcwen1990/p/6656739.html
Copyright © 2011-2022 走看看