如何将Oracle PL/SQL包中的电子邮件发送到多个接收器?

Dee*_*ent 2 oracle plsql oracle11g plsqldeveloper

如何将Oracle PL/SQL包中的电子邮件发送到多个接收器?我在oracle包中有pl/sql程序,它只适用于一个接收器.我需要改进它的功能,让它可以同时向多个接收器发送电子邮件,如"To:David Festool; Peter Makita; John Dewalt".任何身体都可以帮助我,非常感谢!请提供修改后的代码.


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@MyCompany.on.ca');
  utl_smtp.rcpt(c, p_recip);
  utl_smtp.open_data(c);
  send_header('From', '"Title" <Email@MyCompany.on.ca');
  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;
--------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

Ale*_*ole 9

您需要utl_smtp.rcpt多次呼叫,每个接收者一次; 您无法在一次通话中提供值列表.

从UTL_SMTP.RCPT文档:

要向多个收件人发送邮件,请多次调用此例程.每次调用都会将传递计划到单个电子邮件地址.

这意味着您无法真正传递一串名称,除非您乐意解析各个地址; 可能会更容易传递一组值.

所述TO报头是一个单独的问题; 如果我没有记错,这是真的只是用于显示,其地址为一个rcpt,但不是TO(或CC)头是BCC是如何实现的.虽然引用需要......

这是一篇旧的AskTom文章,展示了这一点.jonearles建议使用UTL_MAIL应该进行调查.

  • +1并且你对'BCC`实现是正确的 - 只需调用`RCPT`并且不包括在消息头中. (2认同)