Oracle Apex发送电子邮件

wd2eg0qa  于 2023-06-22  发布在  Oracle
关注(0)|答案(3)|浏览(296)

我使用utl_smtp从我的oracle Apex应用程序发送电子邮件,一切都很正常,在我的邮件服务器更新到TLS 1.2后,我的Oracle包不再工作了!!!我已经测试了添加钱包和其他解决方案,但它不起作用!我得到不同的错误,如:服务不可用或证书验证失败!有谁知道我该怎么解决?或者有没有不需要TLS认证的邮件提供商?

CREATE OR REPLACE PACKAGE BODY send_email
        IS

   -- constants

   c_username VARCHAR2 (50) := 'my_sender';
   c_password VARCHAR2 (50) := 'my_password';

   the_connection UTL_SMTP.connection;

   -- Functions
    FUNCTION build_address_string (p_string IN VARCHAR2, p_rcps IN VARCHAR2, p_rcps_names IN VARCHAR2)
      RETURN VARCHAR2
    IS

        i INTEGER;
        v_recipients VARCHAR2 (5000);
        v_reply UTL_SMTP.reply;
      
      
    BEGIN

        v_recipients := p_string ||  p_rcps ;
        
        v_reply := UTL_SMTP.rcpt (the_connection, p_rcps );

        DBMS_OUTPUT.PUT_LINE ( 'v_reply.code: '|| v_reply.code|| 'UTL_SMTP.rcpt p_recipient');
        DBMS_OUTPUT.PUT_LINE ( 'v_reply.text: '|| v_reply.text);

        DBMS_OUTPUT.PUT_LINE ( 'v_recipients: '|| v_recipients);

        RETURN v_recipients;
    END;

       -- procedures
       --
        ----------------------------------------------------------------------------------------
    PROCEDURE send_email (p_to_rcps IN VARCHAR2, p_to_rcps_names IN VARCHAR2, p_cc_rcps IN VARCHAR2,
      p_cc_rcps_names IN VARCHAR2, p_subject IN VARCHAR2, p_message_body IN VARCHAR2)
    IS
   
        i INTEGER;
        v_adr_to VARCHAR2 (5000);
        v_adr_cc VARCHAR2 (5000);
        v_host_name VARCHAR2 (65);
        v_reply UTL_SMTP.reply;
        v_replies UTL_SMTP.replies;
        v_smtp_port NUMBER;
        v_smtp_server VARCHAR2 (100);
        v_smtp_sender VARCHAR2 (100);
        v_smtp_user VARCHAR2 (100);

    BEGIN

        v_host_name := 'the host name';
        v_smtp_server := 'smtp server name';
        v_smtp_port := 25;
        v_smtp_sender := 'email sender';
        v_smtp_user := 'email user';
      
      
        v_reply := UTL_SMTP.open_connection (v_smtp_server, v_smtp_port, the_connection);

        DBMS_OUTPUT.PUT_LINE ( 'v_reply.code: '|| v_reply.code|| ' open connection reply');
        DBMS_OUTPUT.PUT_LINE ( 'v_reply.text: '|| v_reply.text);

        v_replies := UTL_SMTP.ehlo (the_connection, v_smtp_server);

        i := v_replies.FIRST;

        WHILE (i IS NOT NULL)
        LOOP
            DBMS_OUTPUT.PUT_LINE ( 'count i '|| i|| ' ehlo replies');
            DBMS_OUTPUT.PUT_LINE ( 'v_reply '|| v_replies (i).code);
            DBMS_OUTPUT.PUT_LINE ( 'v_reply '|| v_replies (i).text);

            i := v_replies.NEXT (i);
        END LOOP;

        IF (v_smtp_user != 'ANONYMOUS')
        THEN
        
            -- BEGIN AUTHENTICATION
            v_reply := UTL_SMTP.command (the_connection, 'AUTH LOGIN'); -- should receive a 334 response, prompting for username

            DBMS_OUTPUT.PUT_LINE ( 'v_reply.code: '|| v_reply.code|| ' AUTH LOGIN');
            DBMS_OUTPUT.PUT_LINE ( 'v_reply.text: '|| v_reply.text);

            v_reply := UTL_SMTP.command (the_connection, UTL_ENCODE.text_encode (c_username, 'WE8ISO8859P1', UTL_ENCODE.BASE64)); -- should receive a 334 response, prompting for password

            DBMS_OUTPUT.PUT_LINE ( 'v_reply.code: '|| v_reply.code|| ' username reply');
            DBMS_OUTPUT.PUT_LINE ( 'v_reply.text: '|| v_reply.text);

            v_reply := UTL_SMTP.command (the_connection, UTL_ENCODE.text_encode (c_password, 'WE8ISO8859P1', UTL_ENCODE.BASE64)); -- should receive a 235 response, you are authenticated

            DBMS_OUTPUT.PUT_LINE ( 'v_reply.code: '|| v_reply.code|| ' pwd reply');
            DBMS_OUTPUT.PUT_LINE ( 'v_reply.text: '|| v_reply.text);
            -- END AUTHENTICATION
        ELSE
            v_reply.code := 235;
        END IF;

        IF (v_reply.code = 235)
        THEN
            -- Check the sender
            v_reply := UTL_SMTP.mail (the_connection, v_smtp_sender);
            DBMS_OUTPUT.PUT_LINE ( 'v_reply.code: '|| v_reply.code|| ' UTL_SMTP.mail sender');
            DBMS_OUTPUT.PUT_LINE ( 'v_reply.text: '|| v_reply.text);

            -- Creating Adsresses
            v_adr_to := build_address_string ('To: ', p_to_rcps, p_to_rcps_names);
            DBMS_OUTPUT.PUT_LINE ( 'v_adr_to: '|| v_adr_to);

            v_adr_cc := build_address_string ('Cc: ', p_cc_rcps, p_cc_rcps_names);
            DBMS_OUTPUT.PUT_LINE ( 'v_adr_cc: '|| v_adr_cc);

            -- Writing the data
            v_reply := UTL_SMTP.open_data (the_connection);
            DBMS_OUTPUT.PUT_LINE ( 'v_reply.code: '|| v_reply.code|| ' UTL_SMTP.open_data ');
            DBMS_OUTPUT.PUT_LINE ( 'v_reply.text: '|| v_reply.text);

            UTL_SMTP.write_data(the_connection, 'From: ' || v_host_name || UTL_TCP.crlf);
            UTL_SMTP.write_data(the_connection, 'Subject: ' || NVL (p_subject, '(no subject)') || UTL_TCP.crlf);
            --UTL_SMTP.write_data(the_connection, 'Reply-To: ' || v_adr_to || UTL_TCP.crlf || UTL_TCP.crlf);
            UTL_SMTP.write_data(the_connection, v_adr_to || UTL_TCP.crlf);
            UTL_SMTP.write_data(the_connection, v_adr_cc || UTL_TCP.crlf);
            UTL_SMTP.write_data (the_connection, '' || UTL_TCP.crlf);
            UTL_SMTP.write_data(the_connection, p_message_body || UTL_TCP.crlf || UTL_TCP.crlf);

            -- sending email and closing the connection
            UTL_SMTP.close_data (the_connection);

            v_reply := UTL_SMTP.quit (the_connection);
            DBMS_OUTPUT.PUT_LINE ( 'v_reply.code: '|| v_reply.code|| ' UTL_SMTP.quit');
            DBMS_OUTPUT.PUT_LINE ( 'v_reply.text: '|| v_reply.text);

        ELSE
            DBMS_OUTPUT.PUT_LINE ( 'authentication failure ');

            v_reply := UTL_SMTP.quit (the_connection);
            DBMS_OUTPUT.PUT_LINE ( 'v_reply.code: '|| v_reply.code|| ' UTL_SMTP.quit');
            DBMS_OUTPUT.PUT_LINE ( 'v_reply.text: '|| v_reply.text);
        END IF;

    EXCEPTION
        WHEN UTL_SMTP.transient_error OR UTL_SMTP.permanent_error
        THEN
            BEGIN
                UTL_SMTP.quit (the_connection);

            EXCEPTION
                WHEN UTL_SMTP.transient_error OR UTL_SMTP.permanent_error
                THEN
                    RAISE;
            -- have a connection to the server. The quit call will
            -- raise an exception that we can ignore.
            END;

            raise_application_error (-20000, 'Failed to send mail due to the following error: ' || SQLERRM);
        WHEN OTHERS
        THEN
            RAISE;
    END send_email;

    
    END send_email;
    /
j2cgzkjk

j2cgzkjk1#

使用apex_mail内置包。下面是一个发送“普通”邮件的过程示例,如果还传递了附件名称和CLOB参数值,则发送带有附件的邮件。

CREATE PROCEDURE p_send_mail (i_from         IN VARCHAR2,
                              i_to           IN VARCHAR2,
                              i_cc           IN VARCHAR2,
                              i_bcc          IN VARCHAR2,
                              i_id_email     IN NUMBER,
                              i_subject      IN VARCHAR2,
                              i_text_msg     IN VARCHAR2 DEFAULT NULL,
                              i_attach_name  IN VARCHAR2 DEFAULT NULL,
                              i_attach_mime  IN VARCHAR2 DEFAULT NULL,
                              i_attach_clob  IN CLOB DEFAULT NULL)
IS
   l_id  NUMBER;
BEGIN
   l_id :=
      apex_mail.send (p_from       => i_from,
                      p_to         => i_to,
                      p_cc         => i_cc,
                      p_bcc        => i_bcc,
                      p_replyto    => NULL,
                      p_subj       => i_subject,
                      p_body       => i_text_msg,
                      p_body_html  => i_text_msg);

   IF     i_attach_name IS NOT NULL
      AND i_attach_clob IS NOT NULL
   THEN
      apex_mail.add_attachment (p_mail_id     => l_id,
                                p_filename    => i_attach_name,
                                p_mime_type   => i_attach_mime,
                                p_attachment  => i_attach_clob);
   END IF;
END p_send_mail;

如果你需要一些非常简单的东西,就打电话

apex_mail.send (p_from  => i_from,
                p_to    => i_to,
                p_subj  => i_subject,
                p_body  => i_text_msg);
bfhwhh0e

bfhwhh0e2#

更好的是,你可以使用Apex_mail内置服务来获得简单有效的方法。https://docs.oracle.com/cd/E14373_01/apirefs.32/e13369/apex_mail.htm#AEAPI343

tp5buhyn

tp5buhyn3#

Oracle apex有一个简单的邮件发送选项,没有程序单元。

相关问题