如何使用边界拆分具有多部分/相关MIME类型的电子邮件?

时间:2012-03-05 12:29:44

标签: image oracle email plsql mime

我在StackOverflow上看到这个great question and answer在电子邮件中嵌入图像。不幸的是,回答者没有解释如何用边界拆分电子邮件 - 他说他不知道边界是什么。

这就是我的尝试:

    v_body := '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
  <head>  
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-15"> 
  </head> 
  <body bgcolor="#ffffff" text="#000000"> 
    <img src="data:image/jpg;base64,------------090303020209010600070908' || v_image || '------------090303020209010600070908" /> 
  </body> 
</html>'; 

utl_mail.send('myemail.example.com', 
              'myemail.example.com',
              null,
              null, 
              'Image attachment test',
              v_body,
              'multipart/related; boundary="------------090303020209010600070908"',
              null);   

它将base64字符串作为原始字符发送,而不是将其转换为图像。

然后,我试过了:

    v_body := 'This is a multi-part message in MIME format. 
--------------090303020209010600070908 
Content-Type: text/html; charset=ISO-8859-15 
Content-Transfer-Encoding: 7bit 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
  <head> 

    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-15"> 
  </head> 
  <body bgcolor="#ffffff" text="#000000"> 
    <img src="cid:part1.06090408.01060107" alt=""> 
  </body> 
</html> 

--------------090303020209010600070908 
Content-Type: image/png; 
 name="moz-screenshot.png" 
Content-Transfer-Encoding: base64 
Content-ID: <part1.06090408.01060107> 
Content-Disposition: inline; 
 filename="moz-screenshot.png" 

' || v_image || '

--------------090303020209010600070908-- '; 

utl_mail.send('myemail.example.com', 
              'myemail.example.com',
              null,
              null, 
              'Image attachment test',
              v_body,
              'multipart/related; boundary="------------090303020209010600070908"',
              null);   

此次电子邮件内容不可见。

那么,我们如何使用Oracle中的边界拆分具有多部分/相关MIME类型的电子邮件?

1 个答案:

答案 0 :(得分:1)

我使用过utl_smtp包:

procedure send_email (
    p_image blob
    )
  is
    conn utl_smtp.connection;
    BOUNDARY  VARCHAR2 (256) := '-----090303020209010600070908';
    i         pls_integer;
    len       pls_integer;
    buff_size pls_integer := 57;
    l_raw     raw(57);
  begin

conn := UTL_SMTP.open_connection (smtp_host, smtp_port); UTL_SMTP.helo (conn, smtp_domain); UTL_SMTP.mail (conn, 'myemail@example.com'); UTL_SMTP.rcpt (conn, 'myemail@example.com'); UTL_SMTP.open_data (conn); UTL_SMTP.write_data (conn, 'From' || ': ' || 'myemail@example.com'|| UTL_TCP.CRLF); UTL_SMTP.write_data (conn, 'To' || ': ' || 'myemail@example.com'|| UTL_TCP.CRLF); UTL_SMTP.write_data (conn, 'MIME-Version: 1.0' || CHR (13) || CHR (10)); UTL_SMTP.write_data (conn, 'Content-Transfer-Encoding: 8bit' || CHR (13) || CHR (10)); UTL_SMTP.write_data (conn, 'Subject: image in attachment' || CHR (13) || CHR (10) ) ; UTL_SMTP.write_data (conn, 'Content-Type: multipart/mixed; boundary="' || BOUNDARY || '"' || UTL_TCP.CRLF); UTL_SMTP.write_data (conn, 'X-Mailer:Mailer by Oracle UTL_SMTP'); UTL_SMTP.write_data (conn, UTL_TCP.CRLF); UTL_SMTP.write_data (conn, 'This is a multi-part message in MIME format.' || UTL_TCP.crlf); UTL_SMTP.write_data (conn, '--' || BOUNDARY || UTL_TCP.CRLF ); UTL_SMTP.write_data (conn, 'Content-Type: text/html; charset="windows-1251"'|| UTL_TCP.CRLF ); UTL_SMTP.write_data (conn, UTL_TCP.CRLF); UTL_SMTP.write_data (conn, 'put your html code here'); UTL_SMTP.write_data (conn, UTL_TCP.CRLF); UTL_SMTP.write_data (conn, '--' || BOUNDARY || UTL_TCP.CRLF ); UTL_SMTP.write_data (conn, 'Content-Type: image/jpeg;'|| UTL_TCP.CRLF ); UTL_SMTP.write_data (conn, 'Content-Disposition: inline; filename="image.jpg"' || UTL_TCP.CRLF); UTL_SMTP.write_data (conn, 'Content-Transfer-Encoding' || ': ' || 'base64' || UTL_TCP.CRLF); UTL_SMTP.write_data (conn, 'Content-ID: <part1.06090408.01060107>'); UTL_SMTP.write_data (conn, UTL_TCP.CRLF); UTL_SMTP.write_data (conn, UTL_TCP.CRLF); i := 1; len := dbms_lob.getlength(p_image); while i < len loop dbms_lob.read(p_image, buff_size, i, l_raw); utl_smtp.write_raw_data(conn, utl_encode.base64_encode(l_raw)); utl_smtp.write_data(conn, utl_tcp.crlf); i := i + buff_size; end loop; utl_smtp.write_data(conn, utl_tcp.crlf); UTL_SMTP.write_data (conn, '--' || BOUNDARY || '--' || UTL_TCP.CRLF); UTL_SMTP.write_data (conn, UTL_TCP.CRLF); UTL_SMTP.close_data (conn); UTL_SMTP.quit (conn);

端;