Oracle sends Mail

Source: Internet
Author: User

1. Create a stored procedure for sending messages

CREATE OR REPLACE PROCEDURE send_mail (p_recipient VARCHAR2,--Mail recipient
P_subject VARCHAR2,--Mail header
p_message VARCHAR2--Message body
) is
--The following four variables should be assigned according to the actual mail server
v_mailhost VARCHAR2 (+): = ' 123.125.50.135 ';--SMTP server address
V_user VARCHAR2 (30): = ' ****** ';--user name of the login SMTP server
V_pass VARCHAR2 (20): = ' ****** ';--password to log on to the SMTP server
V_sender VARCHAR2 (): = ' ******@163.com ';--sender's mailbox, general and Ps_user correspondence
v_conn utl_smtp.connection;--Connection to the mail server
v_msg varchar2 (4000);--Mail content
BEGIN
V_conn: = Utl_smtp.open_connection (v_mailhost, +);
Utl_smtp.ehlo (V_conn, V_mailhost);--Is using EHLO () instead of the helo () function
--otherwise reported: ORA-29279:SMTP permanent error: 503 5.5.2 Send Hello first.
Utl_smtp.command (v_conn, ' AUTH login ');--SMTP Server login Check
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) ));
utl_smtp.mail (V_conn, ' < ' | | v_sender | | ' > '); --Set Sender
utl_smtp.rcpt (V_conn, ' < ' | | p_recipient | | ' > '); --Set recipient
--Create message content to send note the header message and the message body are blank lines
v_msg: = ' Date: ' | | To_char (sysdate, ' yyyy mm DD 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-This is preceded by a header message
| | p_message;--This is the message body
Utl_smtp.open_data (V_conn);--Open stream
Utl_smtp.write_raw_data (V_conn, Utl_raw.cast_to_raw (v_msg));--so the title and content can be written in Chinese
Utl_smtp.close_data (V_conn);--Close stream
Utl_smtp.quit (V_conn);---Close connection
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;

2. You need to configure additional permissions to create the stored procedure (the steps created cannot be wrong)

Begin

Dbms_network_acl_admin.create_acl (

ACL = ' sendmail.xml ', --- the name of the access control list created

Description = ' Normal Access ',

Principal = ' CONNECT ',

Is_grant = TRUE,

privilege = ' Connect ',

Start_date = NULL,

End_date = null

);

End

----------------------------------------------------------------

Begin

Dbms_network_acl_admin.add_privilege (ACL = ' sendmail.xml ',

Principal = ' abs_20160104 ',

Is_grant = TRUE,

privilege = ' Connect ',

Start_date = NULL,

end_date = null);

End

----------------------------------------------------------------

Begin

Dbms_network_acl_admin.assign_acl (

ACL = ' Utlpkg.xml ',

Host = ' 123.125.50.135 ', -- access to the server's address

Lower_port =---The lowest port

Upper_port = 25); -- Highest Port

End

----------------------------------------------------------------

--Delete permission file

BEGIN

Dbms_network_acl_admin.drop_acl (ACL = ' sendmail.xml ');

COMMIT;

END;

--Query All-limit documents

SELECT * from Resource_view WHERE any_path like '/sys/acls/%.xml ';

--Querying File network permissions

SELECT host, Lower_port, Upper_port, ACL from DBA_NETWORK_ACLS;

--Querying the user's network permissions

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;

You can now call the stored procedure to send the message.

Oracle sends Mail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.