Use oracle stored procedure to send emails

Source: Internet
Author: User

Create or replace procedure SEND_MAIL
(As_sender in varchar2, -- email sender
As_recp in varchar2, -- email recipient
As_subject in varchar2, -- mail title
As_msg_body in varchar2) -- mail content
IS
Ls_mailhost varchar2 (30): = '***. ***'; -- address or IP
Lc_mail_conn utl_smtp.connection;
Ls_subject varchar2 (100 );
Ls_msg_body varchar2 (20000 );
Ls_username varchar2 (256): = 'hangxf ';
This article is from Sqlclub

Ls_password varchar2 (256): = 'libozxf ';
BEGIN
Lc_mail_conn: = utl_smtp.open_connection (ls_mailhost, 25 );
Utl_smtp.helo (lc_mail_conn, ls_mailhost );
Utl_smtp.command (lc_mail_conn, 'auth login ');
Utl_smtp.command (lc_mail_conn, demo_base64.encode (utl_raw.cast_to_raw (ls_username )));
Utl_smtp.command (lc_mail_conn, demo_base64.encode (utl_raw.cast_to_raw (ls_password )));
Ls_subject: = 'subject: ['| upper (sys_context ('userenv', 'db _ name') |']-'| as_subject;
Ls_msg_body: = as_msg_body;
Utl_smtp.mail (lc_mail_conn, '<' | as_sender | '>'); -- the '<' here must be written; otherwise, a permanent error occurs.
Utl_smtp.rcpt (lc_mail_conn, '<' | as_recp | '>'); -- the '<' here must be written; otherwise, a permanent error occurs.
Utl_smtp.open_data (lc_mail_conn );
This article is from Sqlclub

Ls_msg_body: = 'from: '| as_sender | chr (13) | chr (10) | 'to:' | as_recp | chr (13) | chr (10) | ls_subject |
Chr (13) | chr (10) | chr (13) | chr (10) | ls_msg_body;
Utl_smtp.write_raw_data (lc_mail_conn, utl_raw.cast_to_raw (ls_msg_body); -- in this way, subject writing supports Chinese, but the body content does not support Chinese;
-- Utl_smtp.write_data (lc_mail_conn, ls_msg_body); -- writing subject does not support Chinese
Utl_smtp.close_data (lc_mail_conn );
Utl_smtp.quit (lc_mail_conn );
EXCEPTION
WHEN UTL_SMTP.INVALID_OPERATION THEN
Dbms_output.put_line ('invalid operation ');
WHEN UTL_SMTP.TRANSIENT_ERROR THEN
Dbms_output.put_line ('transient error ');
WHEN UTL_SMTP.PERMANENT_ERROR THEN
Dbms_output.put_line ('permanent error ');

WHEN OTHERS THEN
Dbms_output.put_line ('others ');
End send_mail;

Exec send_mail ('zxf _ feng@f163.com ', 'zxf _ feng@163.com', 'test', 'test ');

 

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.