从存储过程发送电子邮件

时间:2014-10-20 16:32:45

标签: sql oracle email stored-procedures utl-mail

我需要通过oracle数据库pl / sql发送邮件,存储过程必须包括身份验证(用户名/密码)

1 个答案:

答案 0 :(得分:1)

编写电子邮件的存储过程。

 CREATE OR REPLACE PROCEDURE send_mail
        (  sender     IN VARCHAR2,
           recipient  IN VARCHAR2,
           subject    IN VARCHAR2,
           message    IN VARCHAR2)
        IS
          mailhost     VARCHAR2(30) := 
'YOUR.SMTP.SERVER';
          mail_conn    utl_smtp.connection; 

         crlf VARCHAR2( 2 ):= CHR( 13 ) 
|| CHR( 10 );
         mesg VARCHAR2( 1000 );

        BEGIN

          mail_conn := utl_smtp.open_connection
(mailhost, 25);
              mesg:= 'Date: ' || TO_CHAR( SYSDATE, 
'dd Mon yy 
hh24:mi:ss' ) || crlf ||
             'From:  <'||sender||'>' || crlf ||
             'Subject: '||subject || crlf ||
             'To: '||recipient || crlf ||
             '' || crlf || message;
          utl_smtp.helo(mail_conn, mailhost);
          utl_smtp.mail(mail_conn, sender);
          utl_smtp.rcpt(mail_conn, recipient);
          utl_smtp.data(mail_conn, mesg);
          utl_smtp.quit(mail_conn);

        END;

按如下方式调用存储过程:

 begin

 send_mail('sender e-mail','dest e-mail','here 
goes subject','here goes 
message');

end;