Oracle-使用Excel附件发送电子邮件

时间:2019-05-08 08:51:59

标签: sql excel oracle email plsql

我想使用PLSQL通过电子邮件发送Excel文件

我使用XLSX_BUILDER_PKG创建Excel文件,并且工作正常。它会正确创建excel文件。

https://github.com/commi235/xlsx_builder/blob/master/xlsx_builder_pkg.pks

declare
excel BLOB := empty_blob();

begin
  xlsx_builder_pkg.clear_workbook;
  xlsx_builder_pkg.new_sheet;
  xlsx_builder_pkg.cell( 1, 4, 'Start date:', p_alignment => xlsx_builder_pkg.get_alignment( p_horizontal => 'center' ),p_fillId => xlsx_builder_pkg.get_fill('solid','99CCFF'), p_fontId => xlsx_builder_pkg.get_font( 'Calibri',p_bold => true,p_fontsize => 12) );
  xlsx_builder_pkg.cell( 1, 5, sysdate,p_fillId => xlsx_builder_pkg.get_fill('solid','99CC99'),p_alignment => xlsx_builder_pkg.get_alignment( p_horizontal => 'center' ) );
  xlsx_builder_pkg.cell( 4, 4, 'End date:', p_alignment => xlsx_builder_pkg.get_alignment( p_horizontal => 'center' ),p_fillId => xlsx_builder_pkg.get_fill('solid','99CCFF'), p_fontId => xlsx_builder_pkg.get_font( 'Calibri',p_bold => true,p_fontsize => 12) );
  xlsx_builder_pkg.cell( 4, 5, sysdate ,p_fillId => xlsx_builder_pkg.get_fill('solid','99CC99'),p_alignment => xlsx_builder_pkg.get_alignment( p_horizontal => 'center' ));

  xlsx_builder_pkg.save('EXCEL_FILES', 'emp8.xls');
  excel := xlsx_builder_pkg.finish;


 test_send_mail('my@mail.com',
                 'my@mail.com',
                 'Test',
                 'Test',
                 'excel.xls',
                 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                 excel,
                 'myhost',
                 25); 
end;

然后我想发送excel文件作为附件,我使用xlsx_builder_pkg.finish函数获取BLOB,当我发送电子邮件时,它附带了附件,但是excel文件已损坏。

CREATE OR REPLACE PROCEDURE test_send_mail (p_to          IN VARCHAR2,
                                       p_from        IN VARCHAR2,
                                       p_subject     IN VARCHAR2,
                                       p_text_msg    IN VARCHAR2 DEFAULT NULL,
                                       p_attach_name IN VARCHAR2 DEFAULT NULL,
                                       p_attach_mime IN VARCHAR2 DEFAULT NULL,
                                       p_attach_blob IN BLOB DEFAULT NULL,
                                       p_smtp_host   IN VARCHAR2,
                                       p_smtp_port   IN NUMBER DEFAULT 25)
AS
  l_mail_conn   UTL_SMTP.connection;
  l_boundary    VARCHAR2(50) := '----=*#abc1234321cba#*=';
  l_step        PLS_INTEGER  := 12000; -- make sure you set a multiple of 3 not higher than 24573
BEGIN
  l_mail_conn := UTL_SMTP.open_connection(p_smtp_host, p_smtp_port);
  UTL_SMTP.helo(l_mail_conn, p_smtp_host);
  UTL_SMTP.mail(l_mail_conn, p_from);
  UTL_SMTP.rcpt(l_mail_conn, p_to);

  UTL_SMTP.open_data(l_mail_conn);

  UTL_SMTP.write_data(l_mail_conn, 'Date: ' || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') || UTL_TCP.crlf);
  UTL_SMTP.write_data(l_mail_conn, 'To: ' || p_to || UTL_TCP.crlf);
  UTL_SMTP.write_data(l_mail_conn, 'From: ' || p_from || UTL_TCP.crlf);
  UTL_SMTP.write_data(l_mail_conn, 'Subject: ' || p_subject || UTL_TCP.crlf);
  UTL_SMTP.write_data(l_mail_conn, 'Reply-To: ' || p_from || UTL_TCP.crlf);
  UTL_SMTP.write_data(l_mail_conn, 'MIME-Version: 1.0' || UTL_TCP.crlf);
  UTL_SMTP.write_data(l_mail_conn, 'Content-Type: multipart/mixed; boundary="' || l_boundary || '"' || UTL_TCP.crlf || UTL_TCP.crlf);

  IF p_text_msg IS NOT NULL THEN
    UTL_SMTP.write_data(l_mail_conn, '--' || l_boundary || UTL_TCP.crlf);
    UTL_SMTP.write_data(l_mail_conn, 'Content-Type: text/plain; charset="iso-8859-1"' || UTL_TCP.crlf || UTL_TCP.crlf);

    UTL_SMTP.write_data(l_mail_conn, p_text_msg);
    UTL_SMTP.write_data(l_mail_conn, UTL_TCP.crlf || UTL_TCP.crlf);
  END IF;

  IF p_attach_name IS NOT NULL THEN
    UTL_SMTP.write_data(l_mail_conn, '--' || l_boundary || UTL_TCP.crlf);
    UTL_SMTP.write_data(l_mail_conn, 'Content-Type: ' || p_attach_mime || '; name="' || p_attach_name || '"' || UTL_TCP.crlf);
    UTL_SMTP.write_data(l_mail_conn, 'Content-Disposition: attachment; filename="' || p_attach_name || '"' || UTL_TCP.crlf || UTL_TCP.crlf);

    FOR i IN 0 .. TRUNC((DBMS_LOB.getlength(p_attach_blob) - 1 )/l_step) LOOP
      UTL_SMTP.write_data(l_mail_conn, UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(DBMS_LOB.substr(p_attach_blob, l_step, i * l_step + 1))));
    END LOOP;

    UTL_SMTP.write_data(l_mail_conn, UTL_TCP.crlf || UTL_TCP.crlf);
  END IF;

  UTL_SMTP.write_data(l_mail_conn, '--' || l_boundary || '--' || UTL_TCP.crlf);
  UTL_SMTP.close_data(l_mail_conn);

  UTL_SMTP.quit(l_mail_conn);
END;

我使用了不同的MIME类型,但是对于'application / vnd.openxmlformats-officedocument.spreadsheetml.sheet',我的文件已损坏(散列信息),对于'application / vnd.ms-excel',我已经有我打不开的文件

1 个答案:

答案 0 :(得分:1)

将此与我提供的代码进行比较,您似乎缺少了mime标头属性Content-Transfer-Encoding,该属性应为Base64

UTL_SMTP.write_data(l_mail_conn, 'Content-Transfer-Encoding: Base64' || UTL_TCP.crlf);

在附件变得很大或扩展代码以允许多个附件的情况下,要考虑的另一件事是在打开与SMTP服务器的连接之前进行base64编码。否则,在进行转换/写入操作时,您可能会因使会话长时间存活而超时。