您的位置:首页 > 数据库

Send Emails from PL/SQL .

2013-07-09 12:14 260 查看
1.要想从DB Server发邮件,首先得确保DB Server已经启用了SMTP(Simple Mail Transfer Protocol)服务。检查是否启动SMTP的方法

telnet server.domain.com 25

(25是SMTP的默认端口)

2.PL/SQL to send email sample code:

[sql]
view plaincopyprint?

DECLARE  
   lc_from_email_id   VARCHAR2 (100);  
   lc_mailhost        VARCHAR2 (100)      := 'abc.com';  
   lc_connection      UTL_SMTP.connection;  
   v_mail_sub         VARCHAR2 (200)      := 'Hello';  
   v_mail_body        VARCHAR2 (1000)     := 'This is the mail body';  
   v_from_email       VARCHAR2 (100)      := 'ptian@xxx.com';  
   v_to_email         VARCHAR2 (100)      := 'ptian@xxx.com';  
BEGIN  
   lc_connection := UTL_SMTP.open_connection (lc_mailhost, 25);  
   UTL_SMTP.helo (lc_connection, lc_mailhost);  
   UTL_SMTP.mail (lc_connection, v_from_email);  
   UTL_SMTP.rcpt (lc_connection, v_to_email);  
   UTL_SMTP.open_data (lc_connection);  
   UTL_SMTP.write_data (lc_connection, 'Subject' || ':' || v_mail_sub || ' ' || UTL_TCP.crlf);  
   UTL_SMTP.write_data (lc_connection, UTL_TCP.crlf || v_mail_body || UTL_TCP.crlf);  
   UTL_SMTP.close_data (lc_connection);  
   UTL_SMTP.quit (lc_connection);  
EXCEPTION  
   WHEN OTHERS  
   THEN  
      DBMS_OUTPUT.put_line ('Mail not sent');  
END;  

DECLARE
lc_from_email_id   VARCHAR2 (100);
lc_mailhost        VARCHAR2 (100)      := 'abc.com';
lc_connection      UTL_SMTP.connection;
v_mail_sub         VARCHAR2 (200)      := 'Hello';
v_mail_body        VARCHAR2 (1000)     := 'This is the mail body';
v_from_email       VARCHAR2 (100)      := 'ptian@xxx.com';
v_to_email         VARCHAR2 (100)      := 'ptian@xxx.com';
BEGIN
lc_connection := UTL_SMTP.open_connection (lc_mailhost, 25);
UTL_SMTP.helo (lc_connection, lc_mailhost);
UTL_SMTP.mail (lc_connection, v_from_email);
UTL_SMTP.rcpt (lc_connection, v_to_email);
UTL_SMTP.open_data (lc_connection);
UTL_SMTP.write_data (lc_connection, 'Subject' || ':' || v_mail_sub || ' ' || UTL_TCP.crlf);
UTL_SMTP.write_data (lc_connection, UTL_TCP.crlf || v_mail_body || UTL_TCP.crlf);
UTL_SMTP.close_data (lc_connection);
UTL_SMTP.quit (lc_connection);
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ('Mail not sent');
END;




ps:可以通过下边的Query来获取SMTP Server Name

[sql]
view plaincopyprint?

select fscpv.parameter_value smtp_server_name  
from   fnd_svc_comp_params_tl fscpt  
,      fnd_svc_comp_param_vals fscpv  
,      fnd_svc_components fsc  
where  fscpt.parameter_id = fscpv.parameter_id  
and    fscpv.component_id = fsc.component_id  
and    fscpt.display_name = 'Outbound Server Name'  
and    fsc.component_name = 'Workflow Notification Mailer';  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: