# Mail Procedure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| CREATE OR REPLACE PROCEDURE proc_sendmail(p_receiver VARCHAR2, p_subject VARCHAR2, p_message VARCHAR2 ) IS v_smtphost VARCHAR2(30) := 'xxx'; v_smtpport number(5) := 25; v_user VARCHAR2(30) := 'xxx@xxx.com'; v_pass VARCHAR2(20) := 'xxx'; v_sender VARCHAR2(50) := 'xxx@xxx.com'; v_conn UTL_SMTP.connection; v_msg varchar2(4000); v_subject varchar2(4000);
BEGIN v_conn := UTL_SMTP.open_connection(v_smtphost, v_smtpport); UTL_SMTP.ehlo(v_conn, v_smtphost);
UTL_SMTP.mail(v_conn, '<' || v_sender || '>'); UTL_SMTP.rcpt(v_conn, '<' || p_receiver || '>');
l_subject := '=?utf-8?B?'|| utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw(convert(p_subject,'UTF8'))))||'?=';
v_msg := 'Date:' || TO_CHAR(SYSDATE, 'yyyy mm dd hh24:mi:ss') || UTL_TCP.CRLF || 'From: ' || v_sender || '' || UTL_TCP.CRLF || 'To: ' || p_receiver || '' || UTL_TCP.CRLF || 'Subject: ' || v_subject || UTL_TCP.CRLF || UTL_TCP.CRLF || p_message;
UTL_SMTP.open_data(v_conn); utl_smtp.write_raw_data(v_conn, utl_raw.cast_to_raw('Content-Type: text/plain; charset=utf-8' || utl_tcp.CRLF)); 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); UTL_SMTP.quit(v_conn); END proc_sendmail;
|
# Test
sqlplus 连接 Oracle: sqlplus username/password@host:port/instance
SQL> exec proc_sendmail(p_receiver => 'xxx@xxx.com',p_subject => '世界,你好',p_message => '你好啊,世界!');