UTL_MAIL Introduction
UTL_MAIl reference
Security Model
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.
This package is now an invoker’s rights package and the invoking user will need the connect privilege granted in the access control list assigned to the remote network host to which he wants to connect.
Operations
You must both install UTL_MAIL
and define the SMTP_OUT_SERVER
.
■ To install UTL_MAIL:
sqlplus sys/<pwd>
SQL> @$ORACLE_HOME/rdbms/admin/utlmail.sql
SQL> @$ORACLE_HOME/rdbms/admin/prvtmail.plb
■ You define the SMTP_OUT_SERVER
parameter in the init.ora rdbms initialization file. However, if SMTP_OUT_SERVER
is not defined, this invokes a default of DB_DOMAIN
which is guaranteed to be defined to perform appropriately
Rules and Limits
Use UTL_MAIL
only within the context of the ASCII (American Standard Code for Information Interchange) and EBCDIC (Extended Binary-Coded Decimal Interchange Code) codes.
Summary of UTL_MAIL
- SEND: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:Represents the SEND Procedure overloaded for RAW attachments
- SEND_ATTACH_VARCHAR2:Represents the SEND Procedure overloaded for VARCHAR2 attachments
UTL_MAIL in Action
安裝UTL_MAIL包
切換到Oracle身份,以sys使用者登入:
[[email protected]]$ sqlplus /nolog
SQL*Plus: Release 11.2.0.1.0 Production on Wed Jan 23 17:23:38 2013
Copyright (c) 1982, 2009, Oracle. All rights reserved.
SQL> conn /as sysdba
Connected.
SQL> desc UTL_MAIL.SEND
ERROR:
ORA-04043: object UTL_MAIL.SEND does not exist
正如前面提到的, UTL_MAIL
包在安裝資料庫時並不會預設安裝;此時需要手動安裝,安裝過程如下:
切換到UTL_MAIL
包路徑,確認程式碼是否存在:
[[email protected] admin]$ cd $ORACLE_HOME/rdbms/admin
[[email protected] admin]$ pwd
/db/oracle/product/11.2.0/db_1/rdbms/admin
[[email protected] admin]$ ls *mail*
prvtmail.plb utlmail.sql
安裝UTL_MAIL
包:
[[email protected] admin]$ sqlplus /nolog
SQL*Plus: Release 11.2.0.1.0 Production on Wed Jan 23 17:46:24 2013
Copyright (c) 1982, 2009, Oracle. All rights reserved.
SQL> conn /as sysdba
Connected.
SQL> @utlmail.sql
Package created.
Synonym created.
SQL> @prvtmail.plb
Package created.
Package body created.
Grant succeeded.
Package body created.
No errors.
SQL> desc utl_mail
PROCEDURE SEND
Argument Name Type In/Out Default?
------------------------------ ----------------------- ------ --------
SENDER VARCHAR2 IN
RECIPIENTS VARCHAR2 IN
CC VARCHAR2 IN DEFAULT
BCC VARCHAR2 IN DEFAULT
SUBJECT VARCHAR2 IN DEFAULT
MESSAGE VARCHAR2 IN DEFAULT
MIME_TYPE VARCHAR2 IN DEFAULT
PRIORITY BINARY_INTEGER IN DEFAULT
REPLYTO VARCHAR2 IN DEFAULT
PROCEDURE SEND_ATTACH_RAW
Argument Name Type In/Out Default?
------------------------------ ----------------------- ------ --------
SENDER VARCHAR2 IN
RECIPIENTS VARCHAR2 IN
CC VARCHAR2 IN DEFAULT
BCC VARCHAR2 IN DEFAULT
SUBJECT VARCHAR2 IN DEFAULT
MESSAGE VARCHAR2 IN DEFAULT
MIME_TYPE VARCHAR2 IN DEFAULT
PRIORITY BINARY_INTEGER IN DEFAULT
ATTACHMENT RAW IN
ATT_INLINE BOOLEAN IN DEFAULT
ATT_MIME_TYPE VARCHAR2 IN DEFAULT
ATT_FILENAME VARCHAR2 IN DEFAULT
REPLYTO VARCHAR2 IN DEFAULT
PROCEDURE SEND_ATTACH_VARCHAR2
Argument Name Type In/Out Default?
------------------------------ ----------------------- ------ --------
SENDER VARCHAR2 IN
RECIPIENTS VARCHAR2 IN
CC VARCHAR2 IN DEFAULT
BCC VARCHAR2 IN DEFAULT
SUBJECT VARCHAR2 IN DEFAULT
MESSAGE VARCHAR2 IN DEFAULT
MIME_TYPE VARCHAR2 IN DEFAULT
PRIORITY BINARY_INTEGER IN DEFAULT
ATTACHMENT VARCHAR2 IN
ATT_INLINE BOOLEAN IN DEFAULT
ATT_MIME_TYPE VARCHAR2 IN DEFAULT
ATT_FILENAME VARCHAR2 IN DEFAULT
REPLYTO VARCHAR2 IN DEFAULT
給使用者授權
非SYS使用者沒有許可權呼叫UTL_MAIL
:
SQL> disc
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> conn dev
Enter password:
Connected.
SQL> desc utl_mail
ERROR:
ORA-04043: object "SYS"."UTL_MAIL" does not exist
授權給PUBLIC或某一個特定的使用者,以使使用者有許可權呼叫該程式包:
SQL> conn /as sysdba
Connected.
SQL> grant execute on utl_mail to dev;
Grant succeeded.
確認授權後,使用者可以訪問該程式包:
SQL> conn dev
Enter password:
Connected.
SQL> desc utl_mail
PROCEDURE SEND
Argument Name Type In/Out Default?
... ...
郵件伺服器測試
使用telnet測試smtp是否正常
[[email protected] ]$ telnet smtp.test-it.net 25
Trying 192.168.1.111...
Connected to smtp.test-it.net.
Escape character is '^]'.
220 test-it.net (IMail 9.23 574278-2) NT-ESMTP Server X1
EHLO oradb.test-it.net
250-test-it.net says hello
250-SIZE 0
250-8BITMIME
250-DSN
250-ETRN
250-AUTH LOGIN CRAM-MD5
250-AUTH LOGIN
250-AUTH=LOGIN
250 EXPN
MAIL FROM:[email protected]
250 ok
RCPT TO:[email protected]
250 ok its for <[email protected]>
DATA
354 ok, send it; end with <CRLF>.<CRLF>
Subject:telnet smtp test
This is a test message with telnet smtp
.
250 Message queued
quit
221 Goodbye
Connection closed by foreign host.
檢視郵件箱,或者/var/mail/log
確保可以收到以上郵件。
若郵件服務有問題,需先配置好,才能進行UTL_MAIL
的測試。
UTL_MAIl使用示例
傳送第一封郵件
SQL> conn dev
Enter password:
Connected.
SQL> begin
2 utl_mail.send(sender=>'[email protected]',
3 recipients=>'[email protected]',
4 subject=>'utl_mail smtp test',
5 message=>'This is a test message with utl_mail');
6 end;
7 /
begin
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error
ORA-06512: at "SYS.UTL_MAIL", line 654
ORA-06512: at "SYS.UTL_MAIL", line 671
ORA-06512: at line 2
出現以上錯誤,是因為smtp_out_server
沒有設定。
設定smtp_out_server
引數:
SQL> conn /as sysdba
Connected.
SQL> show parameter smtp_out_server
NAME TYPE VALUE
------------------------ ----------- ---------------------------
smtp_out_server string
SQL> alter system set smtp_out_server='smtp.it-test.net';
System altered.
傳送以上同樣的郵件,出現新的錯誤:
ERROR at line 1:
ORA-24247: network access denied by access control list (ACL)
ORA-06512: at "SYS.UTL_MAIL", line 654
ORA-06512: at "SYS.UTL_MAIL", line 671
ORA-06512: at line 2
檢視MOS,原來是Oracle做了ACL限制,需要授權允許使用者DEV訪問外部網路。 執行以下指令碼,授權DEV訪問外部網路(若是其他使用者,則將DEV改為對應賬戶即可):
BEGIN
-- Only uncomment the following line if ACL "network_services.xml" has already been created
--DBMS_NETWORK_ACL_ADMIN.DROP_ACL('network_services.xml');
DBMS_NETWORK_ACL_ADMIN.CREATE_ACL(
acl => 'network_services.xml',
description => 'SMTP ACL',
principal => 'DEV',
is_grant => true,
privilege => 'connect');
DBMS_NETWORK_ACL_ADMIN.ADD_PRIVILEGE(
acl => 'network_services.xml',
principal => 'DEV',
is_grant => true,
privilege => 'resolve');
DBMS_NETWORK_ACL_ADMIN.ASSIGN_ACL(
acl => 'network_services.xml',
host => '*');
COMMIT;
END;
/
執行後,重新執行上述發郵件程式碼,執行無錯誤,檢視收件箱也可以成功收到郵件。到此,使用UTL_MAIL
就成功傳送了第一封郵件。
Reference
- Master Note For PL/SQL
UTL_SMTP
andUTL_MAIL
Packages [ID 1137673.1]