1、简介并安装 UTL_MAIL 包
The UTL_MAIL package is a utility for managing email which includes commonly used email features,
such as attachments, CC, BCC, and return receipt.
UTL_MAIL is not installed by default because of the SMTP_OUT_SERVER configuration requirement
and the security exposure this involves.
In installing UTL_MAIL, you should take steps to prevent the port defined by SMTP_OUT_SERVER
being swamped by data transmissions.
UTL_MAIL 包是一个用来发送邮件的工具,包括常用的电子邮件功能,比如附件,抄送,密送以及回执。由于 SMTP_OUT_SERVER 配置的要求以及安全隐患问题,默认情况下 UTL_MAIL 包是没有安装到Oracle 中的。所以如果需要使用就要额外进行安装。
安装的方法是在 sys 用户下,调用$ORACLE_HOME/rdbms/admin目录下的两个脚本文件。
SQL> connect / as sysdba;
Connected.
SQL> @$ORACLE_HOME/rdbms/admin/utlmail.sql
Package created.
Synonym created.
SQL> @$ORACLE_HOME/rdbms/admin/prvtmail.plb
Package created.
Package body created.
Grant succeeded.
Package body created.
No errors.
这两个脚本的作用就是建立utl_mail工具包。安装完成之后要设置 smtp_out_server 参数。该参数指定的是连接邮件服务器的名称或 IP 地址。
SQL> show parameter smtp
NAME TYPE VALUE
------------------------------------ ----------- ---------------
smtp_out_server string
SQL> alter system set smtp_out_server='192.10.2.55' scope=both;
System altered.
SQL> show parameter smtp
NAME TYPE VALUE
------------------------------------ ----------- ---------------
smtp_out_server string 192.10.2.55
utl_mail 包包含三个方法(send, send_attach_raw, send_attach_varchar2)
SEND Procedure
Packages an email message into the appropriate format, locates SMTP information,
and delivers the message to the SMTP server for forwarding to the recipients
SEND_ATTACH_RAW Procedure
Represents the SEND Procedure overloaded for RAW attachments
SEND_ATTACH_VARCHAR2 Procedure
Represents the SEND Procedure overloaded for VARCHAR2 attachments
如果邮件不用附件的话,我们使用 send 方法就可以了
2、使用 SYS 用户直接调用 utl_mail 包的 send 方法
UTL_MAIL.SEND (
sender IN VARCHAR2 CHARACTER SET ANY_CS,
recipients IN VARCHAR2 CHARACTER SET ANY_CS,
cc IN VARCHAR2 CHARACTER SET ANY_CS DEFAULT NULL,
bcc IN VARCHAR2 CHARACTER SET ANY_CS DEFAULT NULL,
subject IN VARCHAR2 CHARACTER SET ANY_CS DEFAULT NULL,
message IN VARCHAR2 CHARACTER SET ANY_CS,
mime_type IN VARCHAR2 DEFAULT 'text/plain; charset=us-ascii',
priority IN PLS_INTEGER DEFAULT NULL);
具体参数解释如下:
sender The email address of the sender.
recipients The email addresses of the recipient(s), separated by commas.
cc The email addresses of the CC recipient(s), separated by commas, default is NULL
bcc The email addresses of the BCC recipient(s), separated by commas, default is NULL
subject A string to be included as email subject string, default is NULL
message A text message body.
mime_type The mime type of the message, default is 'text/plain; charset=us-ascii'
priority The message priority, default is NULL.
下面在sys下使用utl_mail.send方法发送邮件。
begin
utl_mail.send(sender => '[email protected]',
recipients => '[email protected]',
message => 'abcccc',
subject => 'ddddd');
end;
/
PL/SQL procedure successfully completed
在接受邮箱中发现发送的邮件。
3、非 SYS 用户调用 utl_mail 包的 send 方法配置
如果是非 SYS 用户直接使用utl_mail包,需要将 utl_tcp、utl_mail、utl_smtp 和 dbms_network_acl_admin 四个包的执行权限赋给该用户。
SQL> grant execute on utl_tcp to ava
Grant succeeded.
SQL> grant execute on utl_smtp to ava
Grant succeeded.
SQL> grant execute on utl_mail to ava
Grant succeeded.
SQL> grant execute on dbms_network_acl_admin to ava
Grant succeeded.
此外由于不是超级用户(sys), 所以没有权限去访问外部网络服务(External network service),因此需要配置 Access Control List(ACL)
To configure fine-grained access to external network services, you create an access control list (ACL),
which is stored in Oracle XML DB. You can create the access control list by using Oracle XML DB itself,
or by using the DBMS_NETWORK_ACL_ADMIN and DBMS_NETWORK_ACL_UTILITY PL/SQL packages.
This guide explains how to use these packages to create and manage the access control list.
To create an access control list by using Oracle XML DB and for general conceptual information
about access control lists, see Oracle XML DB Developer's Guide.
Begin
//设置权限项目
DBMS_NETWORK_ACL_ADMIN.CREATE_ACL (acl => 'email_server_permissions.xml',
description => 'Enables network permissions for the e-mail server',
principal => 'AVA',
is_grant => TRUE,
privilege => 'connect');
end;
/
BEGIN
//指定访问主机和相应端口;
DBMS_NETWORK_ACL_ADMIN.ASSIGN_ACL (acl => 'email_server_permissions.xml',
host => '192.10.2.55',
lower_port => 25);
END;
/
之后再使用发送邮件方法
begin
utl_mail.send(sender => '[email protected]',
recipients => '[email protected]',
message => 'abcccc',
subject => 'ddddd');
end;
/
PL/SQL procedure successfully completed
在接受邮箱中发现发送的邮件。
4、封装成一个存储过程调用
CREATE OR REPLACE PROCEDURE EMAIL_MIS(p_message IN varchar2, p_subject IN varchar2) IS
v_sender varchar2(100);
v_receive varchar2(100);
Begin
v_sender := '[email protected]';
v_receive := '[email protected], [email protected]';
utl_mail.send(sender => v_sender,
recipients => v_receive,
message => p_message,
subject => p_subject);
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line('sqlcode : ' ||sqlcode);
DBMS_OUTPUT.put_line('sqlerrm : ' || sqlerrm);
END EMAIL_MIS;
以后可以直接调用存储过程 EMAIL_MIS 就可以了。