Oracle 10G中轻松发送email -- UTL_MAIL

10G之前,如果要从oracle中发送email,必须借助oracle的utl_smtp和utl_tcp功能包,自己包装一个email发送程序,然后使用.自从10G后,这个流程将变得非常简单.因为oracle给我们提供了一个utl_mail包,我们可以直接调用发送email.其实这个包底层也是调用了utl_smtp、utl_tcp的相关api. 不过utl_mail只能用于无安全验证的stmp服务器.如果smtp服务器需要安全验证,只能只用utl_smtp来实现.

1、确定是否安装了utl_mail,如果没有用下面的语句安装utl_mail

  sqlplus sys/
  SQL> @$ORACLE_HOME/rdbms/admin/utlmail.sql
  SQL> @$ORACLE_HOME/rdbms/admin/prvtmail.plb

2、设置smtp_out_server参数

10g中oracle新增加了这个参数, 用来定义smtp服务器.如果该参数没有设置,oracle会自动解析db_domain 参数,用域名来实现mail的发送,没有db_domain也没有设置,那么mail将不会成功发送.建议设置smtp_out_server参数.

  alter system set smtp_out_server='mail.a.com';

如果要同时设置多个smtp服务器,可以将各个服务器用逗号隔开.

  alter system set smtp_out_server='mail.a.com:25,mail.b.com';

上面的域名也可以用ip来代替,oracle默认会使用25端口来发送,也可以手动指定具体的端口.

  alter system set smtp_out_server='mail.a.com:25,mail.b.com:25';

3、测试邮件发送

目前utl_mail提供了三个发送邮件的过程.

 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
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
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

创建存放附件的目录.

  create directory export as '/oracle/product/dump_dir';
  grant read,write on directory export to public;

没有附件的邮件发送示例:

begin
 utl_mail.send(sender=>'oracle@scmdbserver',
               recipients=>'[email protected]',
               subject=>'oracle email test',
               cc=>'[email protected],[email protected]',
               bcc=>'[email protected],[email protected]',
               message=>'sender ok?');
end;
/

带附件的邮件发送(基于文本的附件),如要要发送二进制附件,可以使用utl_mail.send_attach_raw过程

DECLARE
    fHandle utl_file.file_type;
    vTextOut varchar2(32000);
    text varchar2(32000);
BEGIN
    fHandle := UTL_FILE.FOPEN('EXPORT','a.log','r');
    IF UTL_FILE.IS_OPEN(fHandle) THEN
      DBMS_OUTPUT.PUT_LINE('File read open');
    ELSE
     DBMS_OUTPUT.PUT_LINE('File read not open');
    END IF;
    LOOP
     begin
        UTL_FILE.GET_LINE(fHandle,vTextOut);
        text:=text||vTextOut;
     EXCEPTION
        WHEN NO_DATA_FOUND THEN EXIT;
     end;
     END LOOP;
   UTL_FILE.FCLOSE(fHandle);
   utl_mail.send_attach_varchar2(sender=>'oracle@scmdbserver',
                                 recipients=>'[email protected]',
                                 subject=>'oracle email test',
                                 cc=>'[email protected],[email protected]',
                                 bcc=>'[email protected],[email protected]',
                                 message=>'sender ok?',
                                 attachment=>text,
                                 att_filename=>'a.log');

END;
/

oracle在send_attach_varchar2中附件内容用varchar2来存储,也就是说附件不能大于32k.send_attach_raw不能发送超过2000字节的附件.

有关oracle发送mail的信息可以参考,metalink:Doc ID:269375.1 Doc ID:201639.1 FAQ and Known Issues While Using UTL_SMTP and UTL_MAIL Doc:ID 730746.1 其中包含了发送大于32k附件的方法

 

FROM: http://www.validba.net/2009/06/oracle-10g%E4%B8%AD%E8%BD%BB%E6%9D%BE%E5%8F%91%E9%80%81email-%E5%8E%9F%E5%88%9B/

 

Ref: http://hi.baidu.com/javenzhen/blog/item/bed3b74a8f7c63e383025c77.html

你可能感兴趣的:(oracle)