oracle - 如何将 Oracle PL/SQL 包中的电子邮件发送给多个收件人?

标签 oracle plsql oracle11g plsqldeveloper

如何将 Oracle PL/SQL 包中的电子邮件发送给多个收件人?我在 oracle 包中有以下 pl/sql 过程,它只适用于一个接收者。我需要改进它的功能,让它可以同时向多个收件人发送电子邮件,例如“致: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;
--------------------------------------------------------------

最佳答案

您需要多次调用utl_smtp.rcpt,每个收件人一次;您不能在一次调用中给出值列表。

From the UTL_SMTP.RCPT documentation :

To send a message to multiple recipients, call this routine multiple times. Each invocation schedules delivery to a single e-mail address.

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

TO header 是一个单独的问题;如果我没记错的话,那实际上只是为了显示,并且在 TO(或 CC) header 是 BCC 的实现方式。虽然需要引用...

Here's an old AskTom article demonstrating this .不过,应该调查 jonearles 使用 UTL_MAIL 的建议。

关于oracle - 如何将 Oracle PL/SQL 包中的电子邮件发送给多个收件人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17732220/

相关文章:

sql - 将使用加号 (+) 的 Oracle 连接语法转换为标准连接语法

sql - 如何验证Oracle数据库中的SQL查询语法?

sql - plsql使用一个立即执行命令插入多行

java - 从 java 和 sql 查询生成 xml

php - 使用 Php 和 Oracle 显示日期时间。时间失败?

oracle - 如何在oracle PL SQL中进行动态支点

oracle dbms_scheduler repeat_interval

sql - SQL ORA-00933命令未正确结束

oracle - 关于频繁项集的 PL/SQL 问题

oracle - ORA-06530 : Reference to uninitialized composite