UTL_SMTP 带有邮件正文的附件
UTL_SMTP attachment with mail text
我需要使用 Oracle 发送带有附件和文本的电子邮件。我以前用过UTL_MAIL,但是有了这个功能,附件的大小不能超过(我认为)32K。所以我尝试用 UTL_SMTP 发送它,这样更适合附件。
到目前为止,这是我的代码:
c := UTL_SMTP.OPEN_CONNECTION(mailserver);
UTL_SMTP.helo (c, mailserver);
UTL_SMTP.MAIL(c, sFrom);
UTL_SMTP.RCPT(c, sTo);
UTL_SMTP.OPEN_DATA(c);
UTL_SMTP.write_data(c, 'From: ' || sFrom || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'To: ' || sTo || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'Subject: ' || REPLACE(crec.descr, '[DATE]',TO_CHAR(sDATE,'DD.MM.YYYY')) || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'MIME-Version: 1.0' || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'Content-Type: multipart/mixed; boundary="' || c_mime_boundary || '"' || UTL_TCP.crlf);
UTL_SMTP.write_data(c, UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'This is a multi-part message in MIME format.' || UTL_TCP.crlf);
UTL_SMTP.write_data(c, '--' || c_mime_boundary || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'Content-Type: text/plain' || UTL_TCP.crlf);
-- Set up attachment header
UTL_SMTP.write_data(c, 'Content-Disposition: attachment; filename="' || 'your_file_name.csv' || '"' || UTL_TCP.crlf);
UTL_SMTP.write_data(c, UTL_TCP.crlf);
-- Write attachment contents
v_len := DBMS_LOB.getlength(cREPORT);
v_index := 1;
WHILE v_index <= v_len
LOOP
UTL_SMTP.write_data(c, DBMS_LOB.SUBSTR(cREPORT, 32000, v_index));
v_index := v_index + 32000;
END LOOP;
--
-- End attachment
UTL_SMTP.write_data(c, UTL_TCP.crlf);
-- MY TEXT:
UTL_SMTP.write_data(c, 'This is a text.' || UTL_TCP.crlf);
UTL_SMTP.write_data(c, '--' || c_mime_boundary || '--' || UTL_TCP.crlf);
UTL_SMTP.CLOSE_DATA(c);
UTL_SMTP.QUIT(c);
所以带附件的电子邮件就像一个魅力,但电子邮件文本并没有在电子邮件中发送。可以附件+文字发送吗?
我认为你需要复制
UTL_SMTP.write_data(c, 'Content-Type: text/plain' || UTL_TCP.crlf);
以上
UTL_SMTP.write_data(c, 'This is a multi-part message in MIME format.' || UTL_TCP.crlf);
这应该让它知道它期望的文本类型。
干杯
所以,我已经回答了我自己的问题。您必须使用 c_mime_boundary 作为邮件内容和附件内容之间的分隔符。这意味着以下内容:
c := UTL_SMTP.OPEN_CONNECTION(mailserver);
UTL_SMTP.helo (c, mailserver);
UTL_SMTP.MAIL(c, sFrom);
UTL_SMTP.RCPT(c, sTo);
UTL_SMTP.OPEN_DATA(c);
UTL_SMTP.write_data(c, 'From: ' || sFrom || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'To: ' || sTo || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'Subject: ' || REPLACE(crec.descr, '[DATE]',TO_CHAR(sDATE,'DD.MM.YYYY')) || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'MIME-Version: 1.0' || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'Content-Type: multipart/mixed; boundary="' || c_mime_boundary || '"' || UTL_TCP.crlf);
-- Mail body:
UTL_SMTP.write_data(c, '--' || c_mime_boundary || UTL_TCP.crlf); //new
UTL_SMTP.write_data(c, 'Content-Type: text/plain' || UTL_TCP.crlf); //new
UTL_SMTP.write_data(c, 'Text' || UTL_TCP.crlf); //new
-- Set up attachment header
UTL_SMTP.write_data(c, '--' || c_mime_boundary || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'Content-Type: text/plain' || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'Content-Disposition: attachment; filename="' || 'your_file_name.csv' || '"' || UTL_TCP.crlf);
-- Write attachment contents
v_len := DBMS_LOB.getlength(cREPORT);
v_index := 1;
WHILE v_index <= v_len
LOOP
UTL_SMTP.write_data(c, DBMS_LOB.SUBSTR(cREPORT, 32000, v_index));
v_index := v_index + 32000;
END LOOP;
-- End attachment
UTL_SMTP.write_data(c, UTL_TCP.crlf);
UTL_SMTP.write_data(c, '--' || c_mime_boundary || '--' || UTL_TCP.crlf);
UTL_SMTP.CLOSE_DATA(c);
UTL_SMTP.QUIT(c);
我需要使用 Oracle 发送带有附件和文本的电子邮件。我以前用过UTL_MAIL,但是有了这个功能,附件的大小不能超过(我认为)32K。所以我尝试用 UTL_SMTP 发送它,这样更适合附件。
到目前为止,这是我的代码:
c := UTL_SMTP.OPEN_CONNECTION(mailserver);
UTL_SMTP.helo (c, mailserver);
UTL_SMTP.MAIL(c, sFrom);
UTL_SMTP.RCPT(c, sTo);
UTL_SMTP.OPEN_DATA(c);
UTL_SMTP.write_data(c, 'From: ' || sFrom || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'To: ' || sTo || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'Subject: ' || REPLACE(crec.descr, '[DATE]',TO_CHAR(sDATE,'DD.MM.YYYY')) || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'MIME-Version: 1.0' || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'Content-Type: multipart/mixed; boundary="' || c_mime_boundary || '"' || UTL_TCP.crlf);
UTL_SMTP.write_data(c, UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'This is a multi-part message in MIME format.' || UTL_TCP.crlf);
UTL_SMTP.write_data(c, '--' || c_mime_boundary || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'Content-Type: text/plain' || UTL_TCP.crlf);
-- Set up attachment header
UTL_SMTP.write_data(c, 'Content-Disposition: attachment; filename="' || 'your_file_name.csv' || '"' || UTL_TCP.crlf);
UTL_SMTP.write_data(c, UTL_TCP.crlf);
-- Write attachment contents
v_len := DBMS_LOB.getlength(cREPORT);
v_index := 1;
WHILE v_index <= v_len
LOOP
UTL_SMTP.write_data(c, DBMS_LOB.SUBSTR(cREPORT, 32000, v_index));
v_index := v_index + 32000;
END LOOP;
--
-- End attachment
UTL_SMTP.write_data(c, UTL_TCP.crlf);
-- MY TEXT:
UTL_SMTP.write_data(c, 'This is a text.' || UTL_TCP.crlf);
UTL_SMTP.write_data(c, '--' || c_mime_boundary || '--' || UTL_TCP.crlf);
UTL_SMTP.CLOSE_DATA(c);
UTL_SMTP.QUIT(c);
所以带附件的电子邮件就像一个魅力,但电子邮件文本并没有在电子邮件中发送。可以附件+文字发送吗?
我认为你需要复制
UTL_SMTP.write_data(c, 'Content-Type: text/plain' || UTL_TCP.crlf);
以上
UTL_SMTP.write_data(c, 'This is a multi-part message in MIME format.' || UTL_TCP.crlf);
这应该让它知道它期望的文本类型。
干杯
所以,我已经回答了我自己的问题。您必须使用 c_mime_boundary 作为邮件内容和附件内容之间的分隔符。这意味着以下内容:
c := UTL_SMTP.OPEN_CONNECTION(mailserver);
UTL_SMTP.helo (c, mailserver);
UTL_SMTP.MAIL(c, sFrom);
UTL_SMTP.RCPT(c, sTo);
UTL_SMTP.OPEN_DATA(c);
UTL_SMTP.write_data(c, 'From: ' || sFrom || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'To: ' || sTo || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'Subject: ' || REPLACE(crec.descr, '[DATE]',TO_CHAR(sDATE,'DD.MM.YYYY')) || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'MIME-Version: 1.0' || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'Content-Type: multipart/mixed; boundary="' || c_mime_boundary || '"' || UTL_TCP.crlf);
-- Mail body:
UTL_SMTP.write_data(c, '--' || c_mime_boundary || UTL_TCP.crlf); //new
UTL_SMTP.write_data(c, 'Content-Type: text/plain' || UTL_TCP.crlf); //new
UTL_SMTP.write_data(c, 'Text' || UTL_TCP.crlf); //new
-- Set up attachment header
UTL_SMTP.write_data(c, '--' || c_mime_boundary || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'Content-Type: text/plain' || UTL_TCP.crlf);
UTL_SMTP.write_data(c, 'Content-Disposition: attachment; filename="' || 'your_file_name.csv' || '"' || UTL_TCP.crlf);
-- Write attachment contents
v_len := DBMS_LOB.getlength(cREPORT);
v_index := 1;
WHILE v_index <= v_len
LOOP
UTL_SMTP.write_data(c, DBMS_LOB.SUBSTR(cREPORT, 32000, v_index));
v_index := v_index + 32000;
END LOOP;
-- End attachment
UTL_SMTP.write_data(c, UTL_TCP.crlf);
UTL_SMTP.write_data(c, '--' || c_mime_boundary || '--' || UTL_TCP.crlf);
UTL_SMTP.CLOSE_DATA(c);
UTL_SMTP.QUIT(c);