ORACLE 如何配置邮件服务器操作
本文实例讲述了ORACLE 配置邮件服务器操作。分享给大家供大家参考,具体如下:
在Oracle sql developer中执行 必须有Begin END
1 创建 ACL
BEGIN
dbms_network_acl_admin.create_acl(acl => ’email.xml’,
DESCRIPTION => ‘Enables network permissions for the e-mail server’,
principal => ‘C##ESD_MONITOR’, –表示赋予哪个用户 必须大写
is_grant => TRUE,
PRIVILEGE => ‘resolve’,
start_date => NULL,
end_date => NULL);
END;
2 赋予权限
begin
dbms_network_acl_admin.add_privilege(acl => ’email.xml’, –同上xml名称
principal => ‘C##ESD_MONITOR’, –表示赋予哪个用户 必须大写
is_grant => TRUE,
privilege => ‘connect’, –权限名
start_date => null,
end_date => null);
end;
3 设置端口
begin
dbms_network_acl_admin.assign_acl ( — 该段命令意思是允许访问acl名为utl_sendmail.xml下授权的用户,使用oracle网络访问包,所允许访问的目的主机,及其端口范围。
acl => ’email.xml’,
host => ‘*’– , — ip地址或者域名,填写http://localhost:9000/hello与http://localhost:9000/是会报host无效的
— 且建议使用ip地址或者使用域名,若用localhost,当oracle不是安装在本机上的情况下,会出现问题
— lower_port => 9000, — 允许访问的起始端口号
— upper_port => Null — 允许访问的截止端口号
);
end;
4 查询权限设置情况
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;
5 创建邮件发送存储过程
create or replace procedure send_mail(p_recipient VARCHAR2, — 邮件接收人
p_subject VARCHAR2, — 邮件标题
p_message VARCHAR2, — 邮件正文
p_type number — 1文本 2html
) as
–下面四个变量请根据实际邮件服务器进行赋值
v_mailhost VARCHAR2(30) := ‘smtp.qq.com’; –SMTP服务器地址
v_user VARCHAR2(30) := ‘111@qq.com’; –登录SMTP服务器的用户名
v_pass VARCHAR2(20) := ‘111’; –登录SMTP服务器的密码 授权码
v_sender VARCHAR2(50) := ‘111@qq.com’; –发送者邮箱,一般与 ps_user 对应
v_conn UTL_SMTP.connection; –到邮件服务器的连接
v_msg varchar2(4000); –邮件内容
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 || ‘>’); –设置发件人 注:网上很多资料直接写v_sender,这样写会报ORA-29279: SMTP 500 error
UTL_SMTP.rcpt(v_conn, ‘<‘ || p_recipient || ‘>’); –设置收件人
UTL_SMTP.open_data(v_conn); –打开流
if p_type = 1 then
— 创建要发送的邮件内容 注意报头信息和邮件正文之间要空一行
v_msg := ‘Date:’ || TO_CHAR(SYSDATE, ‘dd mon yy 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 — 这前面是报头信息
|| p_message; — 这个是邮件正文
UTL_SMTP.write_raw_data(v_conn, UTL_RAW.cast_to_raw(v_msg)); –这样写标题和内容都能用中文
elsif p_type = 2 then
UTL_SMTP.write_data(v_conn,
‘From:’ || ‘<‘ || v_sender || ‘>’ || utl_tcp.CRLF);
UTL_SMTP.write_data(v_conn,
‘To:’ || ‘<‘ || p_recipient || ‘>’ ||
utl_tcp.crlf);
UTL_SMTP.write_raw_data(v_conn,
UTL_RAW.cast_to_raw(convert(‘Subject:’ ||
p_subject ||
utl_tcp.CRLF,
‘ZHS16GBK’)));
UTL_SMTP.write_raw_data(v_conn,
UTL_RAW.cast_to_raw(convert(‘Content-Type:text/html;charset=GBK’ ||
utl_tcp.CRLF,
‘ZHS16GBK’)));
UTL_SMTP.write_data(v_conn, utl_tcp.CRLF);
UTL_SMTP.write_raw_data(v_conn,
UTL_RAW.cast_to_raw(convert(p_message,
‘ZHS16GBK’))); –这样写标题和内容都能用中文
end if;
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;
更多关于Oracle相关内容感兴趣的读者可查看本站专题:《php+Oracle数据库程序设计技巧总结》
希望本文所述对大家Oracle数据库程序设计有所帮助。