2013-07-18 49 views
2

如何將Oracle PL/SQL包中的電子郵件發送給多個接收者?我有一個oracle包下面的pl/sql過程,它只適用於一個接收器。我需要改進它的功能,讓它可以同時向多個接收者發送電子郵件,如「To:David Festool; Peter Makita; John Dewalt」。任何機構都可以幫助我,將非常感謝!請提供我修改後的代碼。如何將Oracle PL/SQL包中的電子郵件發送給多個接收者?


procedure email(p_recip in varchar2, 
       p_subject in varchar2, 
       p_message in varchar2) is 

    c utl_smtp.connection; 
    msg varchar2(4000); 

    procedure send_header(name in varchar2, header in varchar2) as 
    begin 
    utl_smtp.write_data(c, name || ': ' || header || utl_tcp.crlf); 
    end; 
begin 
    --Open SMTP connection 
    c := utl_smtp.open_connection('ExchangeServerName'); 

    -- Write SMTP header 
    utl_smtp.helo(c, 'ExchangeServerName'); 
    utl_smtp.mail(c, '[email protected]'); 
    utl_smtp.rcpt(c, p_recip); 
    utl_smtp.open_data(c); 
    send_header('From', '"Title" <[email protected]'); 
    send_header('To', p_recip); 
    send_header('Subject', p_subject); 
    send_header('Mime-Version', '1.0'); 
    send_header('Content-Type', 'multipart/mixed; boundary="DMW.Boundary.605592468"'); 

    -- Write MIME boundary line for the message body 
    msg := utl_tcp.crlf || '--DMW.Boundary.605592468' || utl_tcp.crlf || 
     'Content-Type: text/plain' || utl_tcp.crlf || 
     'Content-Transfer-Encoding: 7bit' || utl_tcp.crlf || 
     utl_tcp.crlf; 
    utl_smtp.write_data(c, msg); 

    -- Write message body 
    utl_smtp.write_data(c, p_message || utl_tcp.crlf); 

    -- Clean up 
    utl_smtp.close_data(c); 
    utl_smtp.quit(c); 
exception 
    when utl_smtp.transient_error or utl_smtp.permanent_error then 
    begin 
     utl_smtp.quit(c); 
    exception 
     when utl_smtp.transient_error or utl_smtp.permanent_error then 
     null; 
     -- When the SMTP server is down or unavailable, we don't have 
     -- a connection to the server. The QUIT call will raise an 
     -- exception that we can ignore. 
    end; 

    raise_application_error(-20000, 'Failed to send mail due to the following error: ' || 
          sqlerrm); 
end; 
-------------------------------------------------------------- 
+1

您可以用[UTL_MAIL.SEND]工作正常(http://docs.oracle.com/cd/E11882_01/ appdev.112/e25788/u_mail.htm#i1000954)而不是?然後使用逗號分隔的收件人列表。 –

回答

6

你需要調用utl_smtp.rcpt多次,一次爲每個收件人;你不能在一次調用中給出一個值列表。

From the UTL_SMTP.RCPT documentation

要將信息發送給多個收件人,調用這個程序中處理多個 倍。每個調用都安排將郵件傳遞到單個電子郵件地址。

這意味着你不能真正傳遞一串名稱,除非你很樂意解析出個別地址;可能更容易傳遞一組值。

TO標題是一個單獨的問題;如果我沒有記錯,那實際上只是爲了顯示,並且在TO(或CC)標題中具有地址rcpt而不是而不是是如何實現BCC。引文需要儘管...

Here's an old AskTom article demonstrating this。應該調查jonearles建議使用UTL_MAIL。

+2

+1,你對「BCC」實現是正確的 - 只需調用RCPT並且不包含在消息頭中。 –

1

格式爲:

UTL_MAIL.SEND (sender, recipientlist, cc, bcc, subject, Message, mime_type, priority) 

的recipientlist,CC和BCC的參數都是用逗號分隔的收件人列表,複製,和密件抄送電子郵件地址。

發件人,主題,消息和mime_type參數都是單個項目字段。

0

只要運行以下過程以改變代碼:

v_Mail_Host VARCHAR2(50):= 'uacemail.rxcorp.com'; - 您的主機IP或名稱


執行:

開始
prc_email_send( '[email protected]', - 郵件來源
'[email protected]' ,---收件人
'[email protected]; [email protected]。com', - 抄送清單
'這是郵件主題',
'這是郵件正文');
end;
/

刑事訴訟法:

Create or replace procedure prc_email_send(
v_From  VARCHAR2, 
v_Recipient VARCHAR2, 
v_cc_list varchar2, 
v_Subject VARCHAR2, 
v_Mail_body VARCHAR2 
) 

is 
v_Mail_Host VARCHAR2(50) := 'uacemail.rxcorp.com'; 
v_Mail_Conn utl_smtp.Connection; 

crlf  VARCHAR2(2) := chr(13)||chr(10); 
CC_parties varchar2(2000); 

begin 

v_Mail_Conn := utl_smtp.Open_Connection(v_Mail_Host, 25); 
utl_smtp.Helo(v_Mail_Conn, v_Mail_Host); 
utl_smtp.Mail(v_Mail_Conn, v_From); 

utl_smtp.Rcpt(v_Mail_Conn, v_Recipient); 

for i in (SELECT LEVEL AS id, REGEXP_SUBSTR(v_cc_list, '[^;]+', 1, LEVEL) AS cc_email_name 
      FROM dual 
      CONNECT BY REGEXP_SUBSTR(v_cc_list, '[^;]+', 1, LEVEL) IS NOT NULL) loop 
    CC_parties := CC_parties||';'|| i.cc_email_name; 
    utl_smtp.Rcpt(v_Mail_Conn,i.cc_email_name); 

end loop; 


utl_smtp.Data(v_Mail_Conn, 
       'Date: ' || to_char(sysdate, 'Dy, DD Mon YYYY hh24:mi:ss') || crlf || 
       'From: ' || v_From || crlf || 
       'Subject: '|| v_Subject || crlf || 
       'To: '  || v_Recipient || crlf || 
       'Cc: '  || CC_parties|| crlf || 
       'Content-Type: text/html;' ||crlf || 
       v_Mail_body); 
utl_smtp.Quit(v_mail_conn); 

EXCEPTION 
    WHEN OTHERS THEN 
     BEGIN 
    DBMS_OUTPUT.put_line (
     SUBSTR (
       'Unable to send mail to recipients. Error message: ' 
       || SQLERRM 
       || CHR (10) 
       || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE(), 
       1,255)); 
    UTL_SMTP.quit (v_Mail_Conn); 
    UTL_TCP.close_all_connections; 
    EXCEPTION 
    WHEN UTL_SMTP.transient_error OR UTL_SMTP.permanent_error THEN 
     NULL;  
    END; 
END; 

這是我自己

相關問題