CREATE OR REPLACE PROCEDURE send_mail(
p_recipient VARCHAR2,-- 邮件接收人
p_message VARCHAR2 -- 邮件正文
)
IS
/**
@function:发送邮件
@date:2012-10-23
**/
--下面四个变量请根据实际邮件服务器进行赋值
v_mailhost VARCHAR2(30) := '183.57.48.46' ; --SMTP服务器地址
v_pass VARCHAR2(20) := '123456' ; --登录SMTP服务器的密码
v_sender VARCHAR2(50) := '350430862@qq.com' ; --发送者邮箱,一般与 ps_user 对应
v_conn UTL_SMTP. connection ; --到邮件服务器的连接
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))));
UTL_SMTP.mail(v_conn,v_sender); --设置发件人
UTL_SMTP.rcpt(v_conn,p_recipient); --设置收件人
--DBMS_OUTPUT.PUT_LINE('{Toad_59612783_12}[12] v_msg = ' || v_msg);
v_msg := 'Date:' || TO_CHAR(SYSDATE,'dd mon yy hh24:mi:ss' )
|| UTL_TCP.CRLF || 'From: ' || v_sender || '<' || v_sender || '>'
|| UTL_TCP.CRLF || 'To: ' || p_recipient || '<' || p_recipient || '>'
|| UTL_TCP.CRLF || 'Subject: ' || TO_CHAR(SYSDATE,'yyyy-mm-dd hh24:mi:ss' )|| p_subject
|| UTL_TCP.CRLF || UTL_TCP.CRLF -- 这前面是报头信息
|| p_message;
--DBMS_OUTPUT.PUT_LINE('{Toad_59612783_12}[12] v_msg = ' || v_msg);
UTL_SMTP.open_data(v_conn); --打开流
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;
原文链接:https://www.f2er.com/oracle/207744.html