以下的文章主要是以实例演示的方式来讲解如何正确的执行Oracle(Oracle培训 发送邮件,以下的文章就就是对其实际的操作步骤的讲解,如果你对其相关的实际操作有兴趣的话,你就可以对以下的文章点击观看了。

  执行Oracle发送邮件的正确操作步骤:

  LINUX AS3+Oracle 9.2||10.20.

  从Oracle发送邮件:

  示例如下:

  具体的测试环境:LINUX AS3 , Oracle 9.0.2.4

  1.SQL》 select * from v$version;

  2.BANNER

  3.Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production

  4.PL/SQL Release 9.2.0.4.0 - Production

  5.CORE 9.2.0.3.0 Production

  6.TNS for Linux: Version 9.2.0.4.0 - Production

  7.NLSRTL Version 9.2.0.4.0 - Production

  8.SQL》 select * from v$version;

  9.Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod

  10.PL/SQL Release 10.2.0.1.0 - Production

  11.CORE 10.2.0.1.0 Production

  12.TNS for Linux: Version 10.2.0.1.0 - Production

  13.NLSRTL Version 10.2.0.1.0 - Production

  MAIL服务器为WIN2003,WINMAIL

  1.保证ORACLE服务器到MAIL服务器网络畅通,25端口打开

  2.创建Oracle发送邮件的procedure如下:

  1.CREATE OR REPLACE PROCEDURE SEND_MAIL

  (as_sender in varchar2, --邮件发送者

  as_recp in varchar2, --邮件接收者

  as_subject in varchar2, --邮件标题

  as_msg_body in varchar2) --邮件内容

  1.IS

  2.ls_mailhost varchar2(30) := ‘mail server’; -- address or IP

  3.lc_mail_conn utl_smtp.connection;

  4.ls_subject varchar2(100);

  5.ls_msg_body varchar2(20000);

  6.ls_username varchar2(256) := ‘usercode’;

  7.ls_password varchar2(256) := ‘password’;

  8.BEGIN

  9.lc_mail_conn := utl_smtp.open_connection(ls_mailhost, 25);

  10.utl_smtp.helo(lc_mail_conn, ls_mailhost);

  11.utl_smtp.command(lc_mail_conn, ‘AUTH LOGIN’);

  12.utl_smtp.command(lc_mail_conn, demo_base64.encode(utl_raw.cast_to_raw(ls_username)));

  13.utl_smtp.command(lc_mail_conn, demo_base64.encode(utl_raw.cast_to_raw(ls_password)));

  14.ls_subject := ‘Subject: [’ || upper(sys_context(‘userenv’, ‘db_name’)) || ‘] - ’ || as_subject;

  15.ls_msg_body := as_msg_body;

  utl_smtp.mail(lc_mail_conn, ‘《’||as_sender||‘》’); --这里的‘《’ 一定要写,不然会出现permanent error

  utl_smtp.rcpt(lc_mail_conn, ‘《’||as_recp||‘》’);--这里的‘《’ 一定要写,不然会出现permanent error

  1.utl_smtp.open_data(lc_mail_conn);

  2.ls_msg_body := ‘From: ’ || as_sender || chr(13) || chr(10) || ‘To: ’ || as_recp || chr(13) || chr(10) || ls_subject ||

  3.chr(13) || chr(10) || chr(13) || chr(10) || ls_msg_body;

  utl_smtp.write_raw_data(lc_mail_conn, utl_raw.cast_to_raw(ls_msg_body)); --这样写subject可以支持中文但body内容不支持中文;

  -- utl_smtp.write_data(lc_mail_conn, ls_msg_body); --这样写subject不支持中文

  1.utl_smtp.close_data(lc_mail_conn);

  2.utl_smtp.quit(lc_mail_conn);

  3.EXCEPTION

  4.WHEN UTL_SMTP.INVALID_OPERATION THEN

  5.dbms_output.put_line(‘invalid operation’);

  6.WHEN UTL_SMTP.TRANSIENT_ERROR THEN

  7.dbms_output.put_line(‘transient error’);

  8.WHEN UTL_SMTP.PERMANENT_ERROR THEN

  9.dbms_output.put_line(‘permanent error’);

  10.WHEN OTHERS THEN

  11.dbms_output.put_line(‘others’);

  12.end send_mail;

  13.

  3.执行Oracle发送邮件:

  1.exec send_mail(‘heyu@163.net’,‘admin@163.net’,‘我我’,‘this is a oracle test mail’);

  注意事项:上面的过程如果在编译中出现demo_base64.encode must be declared,请大家创建下面的包和包体;

  1.CREATE OR REPLACE PACKAGE demo_base64 IS

  2.-- Base64-encode a piece of binary data.

  3.--

  4.-- Note that this encode function does not split the encoded text into

  5.-- multiple lines with no more than 76 bytes each as required by

  6.-- the MIME standard.

  以上的相关内容就是对Oracle发送邮件的介绍,望你能有所收获。