如何使用PL / SQL发送电子邮件?

时间:2014-09-08 08:04:42

标签: oracle email stored-procedures plsql

嗨朋友我有一项任务,我的任务是编写一个pl / sql程序,用于发送使用gmail作为邮件服务器的电子邮件。

我写了这个程序

CREATE OR REPLACE PROCEDURE send_mail (p_to        IN VARCHAR2,
                                       p_from      IN VARCHAR2,
                                       p_message   IN VARCHAR2,
                                       p_smtp_host IN VARCHAR2,
                                       p_smtp_domain IN varchar2, 
                                       p_smtp_port IN NUMBER DEFAULT 465) AS
  l_mail_conn   UTL_SMTP.connection;
BEGIN
  l_mail_conn := UTL_SMTP.open_connection(p_smtp_host, p_smtp_port);
  UTL_SMTP.ehlo(l_mail_conn, p_smtp_domain);  
  UTL_SMTP.command(l_mail_conn,'AUTH LOGIN');  
  UTL_SMTP.command(l_mail_conn,utl_encode.base64_encode(utl_raw.cast_to_raw('mygmail@gmail.com')));
  UTL_SMTP.command(l_mail_conn,utl_encode.base64_encode(utl_raw.cast_to_raw('mypassword')));
  UTL_SMTP.mail(l_mail_conn, p_from);
  UTL_SMTP.rcpt(l_mail_conn, p_to);
  UTL_SMTP.data(l_mail_conn, p_message || UTL_TCP.crlf || UTL_TCP.crlf);
  UTL_SMTP.quit(l_mail_conn);
END;
/

程序已成功创建

为了执行此程序,我使用了以下代码

BEGIN
  send_mail(p_to        => 'someone@gmail.com',
            p_from      => 'mygmail@gmail.com',
            p_message   => 'This is a test message.',
            p_smtp_host => 'smtp.gmail.com',
        p_smtp_domain => 'gmail.com'
);
END;
/

执行后我收到了以下错误

ERROR at line 1:
ORA-29278: SMTP transient error: 421 Service not available
ORA-06512: at "SYS.UTL_SMTP", line 17
ORA-06512: at "SYS.UTL_SMTP", line 96
ORA-06512: at "SYS.UTL_SMTP", line 138
ORA-06512: at "ZAHID.SEND_MAIL", line 11
ORA-06512: at line 2

如果我更改了端口号25,那么我收到了这个错误:

ERROR at line 1:
ORA-29279: SMTP permanent error: 530 5.7.0 Must issue a STARTTLS command first. t9sm10042472wjf.41
- gsmtp
ORA-06512: at "SYS.UTL_SMTP", line 17
ORA-06512: at "SYS.UTL_SMTP", line 98
ORA-06512: at "SYS.UTL_SMTP", line 158
ORA-06512: at "ZAHID.SEND_MAIL", line 13
ORA-06512: at line 2

我的oracle版本是Oracle9i,我搜索了很多上述错误的解决方案但没有成功,请查看我的代码并帮助我解决它,我将非常感谢你。

1 个答案:

答案 0 :(得分:0)

您认为421 Service not available error是什么?您需要提供smtp凭据。

Oracle只是受害者,罪魁祸首是邮件服务器配置。做:

ALTER SYSTEM SET smtp_out_server = 'your_config' scope=spfile;

然后提供适当的凭据。尝试使用测试电子邮件。看看http://www.orafaq.com/forum/m/588498/?srch=421+Service+not+available#msg_588498

总而言之,您的SMTP服务器未启动。也许,我上面不清楚,

ORA-29278: SMTP transient error: 421 Service not available表示凭据不正确。

ORA-29279: SMTP permanent error: 530 5.7.0 Must issue a STARTTLS command first. t9sm10042472wjf.41 - gsmtp表示SMTP服务器未启动。需要STARTTLS命令。


更新:伙计们,我真诚的道歉,我完全错过了OP已经提到他在9i上。