CREATE
OR
REPLACE
PROCEDURE
send_mail(
2 p_recipient VARCHAR2 , -- 邮件接收人
3 p_subject VARCHAR2 , -- 邮件标题
4 p_message VARCHAR2 -- 邮件正文
5 )
6 IS
7
8 -- 下面四个变量请根据实际邮件服务器进行赋值
9 v_mailhost VARCHAR2 ( 30 ) : = ' smtp.XXX.com ' ; -- SMTP服务器地址
10 v_user VARCHAR2 ( 30 ) : = ' user ' ; -- 登录SMTP服务器的用户名
11 v_pass VARCHAR2 ( 20 ) : = ' pwd ' ; -- 登录SMTP服务器的密码
12 v_sender VARCHAR2 ( 50 ) : = ' user@XXX.com ' ; -- 发送者邮箱,一般与 ps_user 对应
13
14 v_conn UTL_SMTP. connection ; -- 到邮件服务器的连接
15 v_msg varchar2 ( 4000 ); -- 邮件内容
16
17 BEGIN
18
19 v_conn : = UTL_SMTP.open_connection(v_mailhost, 25 );
20 UTL_SMTP.ehlo(v_conn, v_mailhost); -- 是用 ehlo() 而不是 helo() 函数
21 -- 否则会报:ORA-29279: SMTP 永久性错误: 503 5.5.2 Send hello first.
22
23 UTL_SMTP.command(v_conn, ' AUTH LOGIN ' ); -- smtp服务器登录校验
24 UTL_SMTP.command(v_conn,UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(v_user))));
25 UTL_SMTP.command(v_conn,UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(v_pass))));
26
27 UTL_SMTP.mail(v_conn, v_sender); -- 设置发件人
28 UTL_SMTP.rcpt(v_conn, p_recipient); -- 设置收件人
29
30 -- 创建要发送的邮件内容 注意报头信息和邮件正文之间要空一行
31 v_msg : = ' Date: ' || TO_CHAR(SYSDATE, ' dd mon yy hh24:mi:ss ' )
32 || UTL_TCP.CRLF || ' From: ' || ' < ' || v_sender || ' > '
33 || UTL_TCP.CRLF || ' To: ' || ' < ' || p_recipient || ' > '
34 || UTL_TCP.CRLF || ' Subject: ' || p_subject
35 || UTL_TCP.CRLF || UTL_TCP.CRLF -- 这前面是报头信息
36 || p_message; -- 这个是邮件正文
37
38 UTL_SMTP.open_data(v_conn); -- 打开流
39 UTL_SMTP.write_raw_data(v_conn, UTL_RAW.cast_to_raw(v_msg)); -- 这样写标题和内容都能用中文
40 UTL_SMTP.close_data(v_conn); -- 关闭流
41 UTL_SMTP.quit(v_conn); -- 关闭连接
42
43 EXCEPTION
44
45 WHEN OTHERS THEN
46 DBMS_OUTPUT.put_line(DBMS_UTILITY.format_error_stack);
47 DBMS_OUTPUT.put_line(DBMS_UTILITY.format_call_stack);
48
49 END send_mail;
2 p_recipient VARCHAR2 , -- 邮件接收人
3 p_subject VARCHAR2 , -- 邮件标题
4 p_message VARCHAR2 -- 邮件正文
5 )
6 IS
7
8 -- 下面四个变量请根据实际邮件服务器进行赋值
9 v_mailhost VARCHAR2 ( 30 ) : = ' smtp.XXX.com ' ; -- SMTP服务器地址
10 v_user VARCHAR2 ( 30 ) : = ' user ' ; -- 登录SMTP服务器的用户名
11 v_pass VARCHAR2 ( 20 ) : = ' pwd ' ; -- 登录SMTP服务器的密码
12 v_sender VARCHAR2 ( 50 ) : = ' user@XXX.com ' ; -- 发送者邮箱,一般与 ps_user 对应
13
14 v_conn UTL_SMTP. connection ; -- 到邮件服务器的连接
15 v_msg varchar2 ( 4000 ); -- 邮件内容
16
17 BEGIN
18
19 v_conn : = UTL_SMTP.open_connection(v_mailhost, 25 );
20 UTL_SMTP.ehlo(v_conn, v_mailhost); -- 是用 ehlo() 而不是 helo() 函数
21 -- 否则会报:ORA-29279: SMTP 永久性错误: 503 5.5.2 Send hello first.
22
23 UTL_SMTP.command(v_conn, ' AUTH LOGIN ' ); -- smtp服务器登录校验
24 UTL_SMTP.command(v_conn,UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(v_user))));
25 UTL_SMTP.command(v_conn,UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(v_pass))));
26
27 UTL_SMTP.mail(v_conn, v_sender); -- 设置发件人
28 UTL_SMTP.rcpt(v_conn, p_recipient); -- 设置收件人
29
30 -- 创建要发送的邮件内容 注意报头信息和邮件正文之间要空一行
31 v_msg : = ' Date: ' || TO_CHAR(SYSDATE, ' dd mon yy hh24:mi:ss ' )
32 || UTL_TCP.CRLF || ' From: ' || ' < ' || v_sender || ' > '
33 || UTL_TCP.CRLF || ' To: ' || ' < ' || p_recipient || ' > '
34 || UTL_TCP.CRLF || ' Subject: ' || p_subject
35 || UTL_TCP.CRLF || UTL_TCP.CRLF -- 这前面是报头信息
36 || p_message; -- 这个是邮件正文
37
38 UTL_SMTP.open_data(v_conn); -- 打开流
39 UTL_SMTP.write_raw_data(v_conn, UTL_RAW.cast_to_raw(v_msg)); -- 这样写标题和内容都能用中文
40 UTL_SMTP.close_data(v_conn); -- 关闭流
41 UTL_SMTP.quit(v_conn); -- 关闭连接
42
43 EXCEPTION
44
45 WHEN OTHERS THEN
46 DBMS_OUTPUT.put_line(DBMS_UTILITY.format_error_stack);
47 DBMS_OUTPUT.put_line(DBMS_UTILITY.format_call_stack);
48
49 END send_mail;
【补充】邮件内容支持HTML方式如下:
1
UTL_SMTP.open_data(v_conn);
--
打开流
2
3 UTL_SMTP.write_data(v_conn, ' From: ' || v_sender || utl_tcp.CRLF);
4 UTL_SMTP.write_data(v_conn, ' To: ' || p_recipient || utl_tcp.crlf);
5 UTL_SMTP.write_raw_data(v_conn, UTL_RAW.cast_to_raw( convert ( ' Subject: ' || p_subject || utl_tcp.CRLF, ' ZHS16GBK ' )));
6
7 UTL_SMTP.write_raw_data(v_conn, UTL_RAW.cast_to_raw( convert ( ' Content-Type:text/html;charset=GBK ' || utl_tcp.CRLF, ' ZHS16GBK ' )));
8 UTL_SMTP.write_data(v_conn, utl_tcp.CRLF);
9 UTL_SMTP.write_raw_data(v_conn, UTL_RAW.cast_to_raw( convert (p_message, ' ZHS16GBK ' ))); -- 这样写标题和内容都能用中文
10 UTL_SMTP.close_data(v_conn); -- 关闭流
11 UTL_SMTP.quit(v_conn); -- 关闭连接
2
3 UTL_SMTP.write_data(v_conn, ' From: ' || v_sender || utl_tcp.CRLF);
4 UTL_SMTP.write_data(v_conn, ' To: ' || p_recipient || utl_tcp.crlf);
5 UTL_SMTP.write_raw_data(v_conn, UTL_RAW.cast_to_raw( convert ( ' Subject: ' || p_subject || utl_tcp.CRLF, ' ZHS16GBK ' )));
6
7 UTL_SMTP.write_raw_data(v_conn, UTL_RAW.cast_to_raw( convert ( ' Content-Type:text/html;charset=GBK ' || utl_tcp.CRLF, ' ZHS16GBK ' )));
8 UTL_SMTP.write_data(v_conn, utl_tcp.CRLF);
9 UTL_SMTP.write_raw_data(v_conn, UTL_RAW.cast_to_raw( convert (p_message, ' ZHS16GBK ' ))); -- 这样写标题和内容都能用中文
10 UTL_SMTP.close_data(v_conn); -- 关闭流
11 UTL_SMTP.quit(v_conn); -- 关闭连接