我有一个CLOB数据类型,我在那里硬编码HTML代码,然后我想将其压缩。稍后将其作为附件发送。 目前我的代码将ZIP文件作为附件发送,但它没有打开。
注意:可能需要将CLOB转换为BLOB然后将其压缩并放大然后作为附件发送。
有人可以帮我拿样品。
提前致谢
这是我的代码:
DECLARE
p_from VARCHAR2 (2000);
p_to2 VARCHAR2 (2000) := 'xyz@abc.com';
p_subject VARCHAR2 (2000);
p_smtp_host VARCHAR2 (2000);
p_smtp_port NUMBER := 25;
l_mail_conn UTL_SMTP.connection;
l_boundary VARCHAR2 (50) := '----=*#abc1234321cba#*=';
atch_name VARCHAR2 (50);
atch_mine VARCHAR2 (50) := 'application/zip';
atch_clob NCLOB;
l_step PLS_INTEGER := 12000; -- make sure you set a multiple of 3 not higher than 24573
dbname VARCHAR2 (50);
hostname VARCHAR2 (50);
BEGIN
SELECT host into p_smtp_host FROM dba_network_acls where acl = '/sys/acls/utl_mail.xml' and rownum = 1;
if p_smtp_host is not null then
select host_name into hostname from v$instance;
select name into dbname from v$database;
p_from := dbname || '@' || hostname;
p_subject := 'Test Mail';
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_To2);
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_to2 || 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/alternative; boundary="' || l_boundary || '"' || UTL_TCP.crlf || UTL_TCP.crlf );
UTL_SMTP.write_data (l_mail_conn, '--' || l_boundary || UTL_TCP.crlf);
UTL_SMTP.write_data (l_mail_conn, 'Content-Type: text/html; charset="iso-8859-1"' || UTL_TCP.crlf || UTL_TCP.crlf);
atch_clob := '<br><b><font size="2.5">Thanks,</font></b>';
-- NAME
atch_name := 'test.zip';
-- ATTACHMENT
UTL_SMTP.write_data (l_mail_conn, UTL_TCP.crlf || UTL_TCP.crlf);
UTL_SMTP.write_data (l_mail_conn, ('--' || l_boundary || UTL_TCP.crlf));
UTL_SMTP.write_data (l_mail_conn, ('Content-Type: ' || atch_mine || '; name="' || atch_name || '"' || UTL_TCP.crlf));
UTL_SMTP.write_data (l_mail_conn, ('Content-Disposition: attachment; filename="' || atch_name || '"' || UTL_TCP.crlf || UTL_TCP.crlf));
UTL_SMTP.write_data (l_mail_conn, atch_clob);
UTL_SMTP.write_data (l_mail_conn, UTL_TCP.crlf || UTL_TCP.crlf);
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);
else
dbms_output.put_line('Mailing Privilege not avaialble');
END IF;
END;
/
答案 0 :(得分:1)
您的代码中缺少从lob获取数据的内容,请检查下面的代码,我在其中发送pdf文件作为BLOB数据类型的附件:
PROCEDURE Send_Email(ifrom In varchar2, ito In varchar2, icc In varchar2, ifile In varchar2, ifile2 In varchar2,
isubject In varchar2, imsg In varchar2, isrl_no In number)
Is
/*utl_smtp related variables. */
vfile varchar2(100);
v_connection_handle Utl_Smtp.CONNECTION;
v_from_email_address VARCHAR2(1000) := ifrom;
v_to_email_address VARCHAR2(1000) := ito;
v_smtp_host VARCHAR2(100) := 'mail.urdomain.com'; --your mail server
v_subject VARCHAR2(1000) := isubject;
l_message VARCHAR2(2000) := imsg;
nls_charset varchar2(255);
/* this send_header procedure is written in the documentation */
PROCEDURE send_header(pi_name In VARCHAR2, pi_header In VARCHAR2) As
BEGIN
Utl_Smtp.WRITE_DATA(v_connection_handle,
pi_name || ': ' || pi_header || Utl_Tcp.CRLF);
END;
BEGIN
Select Value
Into nls_charset
From NLS_DATABASE_PARAMETERS
Where parameter = 'NLS_CHARACTERSET';
/*utl_smtp related coding. */
v_connection_handle := Utl_Smtp.OPEN_CONNECTION(v_smtp_host, 25);
Utl_Smtp.EHLO(v_connection_handle, v_smtp_host);
utl_smtp.command(v_connection_handle, 'auth login');
utl_smtp.command(v_connection_handle,utl_encode.text_encode('yourid@urdomain.com', nls_charset, 1));
utl_smtp.command(v_connection_handle, utl_encode.text_encode('urpassword', nls_charset, 1));
Utl_Smtp.Mail(v_connection_handle, v_from_email_address);
Utl_Smtp.RCPT(v_connection_handle, v_to_email_address);
IF icc Is Not Null Then
Utl_Smtp.RCPT(v_connection_handle, icc);
END IF;
Utl_Smtp.OPEN_DATA(v_connection_handle);
send_header('From', ifrom);
send_header('To', ito);
IF icc Is Not Null Then
send_header('Cc', icc);
END IF;
send_header('Subject', v_subject);
IF IFILE Is Null And IFILE2 Is Null Then
Utl_Smtp.WRITE_DATA(v_connection_handle,
'Content-Type: text/html;' || Utl_Tcp.CRLF);
Utl_Smtp.WRITE_DATA(v_connection_handle,
' charset=US-ASCII' || Utl_Tcp.CRLF);
Utl_Smtp.WRITE_DATA(v_connection_handle, Utl_Tcp.CRLF);
Utl_Smtp.WRITE_DATA(v_connection_handle, l_message || Utl_Tcp.CRLF);
Utl_Smtp.WRITE_DATA(v_connection_handle, Utl_Tcp.CRLF);
END IF;
--mime header.
IF ifile Is Not Null Then
For i In 1..2 LOOP
/*preparing the lob from file for attachment. */
/*lob operation related varriables */
IF i = 1 Then
vfile := ifile;
Else
IF ifile2 Is Null Then
EXIT;
Else
vfile := ifile2;
END IF;
END IF;
DECLARE
v_src_loc BFILE := Bfilename('EMAIL_FILES', Nvl(vfile, 'temp'));
l_buffer RAW(54);
l_amount BINARY_INTEGER := 54;
l_pos INTEGER := 1;
l_blob BLOB := Empty_Blob;
l_blob_len INTEGER;
v_amount INTEGER;
BEGIN
Dbms_Lob.OPEN(v_src_loc, Dbms_Lob.LOB_READONLY); --read the file
Dbms_Lob.CREATETEMPORARY(l_blob, TRUE); --create temporary lob to store the file.
v_amount := Dbms_Lob.GETLENGTH(v_src_loc); --amount to store.
Dbms_Lob.LOADFROMFILE(l_blob, v_src_loc, v_amount); -- loading from file into temporary lob
l_blob_len := Dbms_Lob.getlength(l_blob);
Utl_Smtp.WRITE_DATA(v_connection_handle,
'MIME-Version: 1.0' || Utl_Tcp.CRLF);
Utl_Smtp.WRITE_DATA(v_connection_handle,
'Content-Type: multipart/mixed; ' || Utl_Tcp.CRLF);
Utl_Smtp.WRITE_DATA(v_connection_handle,
' boundary= "' || 'EMAIL_FILES.SECBOUND' || '"' ||
Utl_Tcp.CRLF);
Utl_Smtp.WRITE_DATA(v_connection_handle, Utl_Tcp.CRLF);
IF i = 1 Then
-- mail body
Utl_Smtp.WRITE_DATA(v_connection_handle,
'--' || 'EMAIL_FILES.SECBOUND' || Utl_Tcp.CRLF);
Utl_Smtp.WRITE_DATA(v_connection_handle,
'Content-Type: text/html;' || Utl_Tcp.CRLF);
Utl_Smtp.WRITE_DATA(v_connection_handle,
' charset=US-ASCII' || Utl_Tcp.CRLF);
Utl_Smtp.WRITE_DATA(v_connection_handle, Utl_Tcp.CRLF);
Utl_Smtp.WRITE_DATA(v_connection_handle, l_message || Utl_Tcp.CRLF);
Utl_Smtp.WRITE_DATA(v_connection_handle, Utl_Tcp.CRLF);
END IF;
-- mail attachment
Utl_Smtp.WRITE_DATA(v_connection_handle,
'--' || 'EMAIL_FILES.SECBOUND' || Utl_Tcp.CRLF);
Utl_Smtp.WRITE_DATA(v_connection_handle,
'Content-Type: application/octet-stream' ||
Utl_Tcp.CRLF);
Utl_Smtp.WRITE_DATA(v_connection_handle,
'Content-Disposition: attachment; ' || Utl_Tcp.CRLF);
Utl_Smtp.WRITE_DATA(v_connection_handle,
' filename="' || vfile || '"' || --my filename
Utl_Tcp.CRLF);
Utl_Smtp.WRITE_DATA(v_connection_handle,
'Content-Transfer-Encoding: base64' || Utl_Tcp.CRLF);
Utl_Smtp.WRITE_DATA(v_connection_handle, Utl_Tcp.CRLF);
/* writing the blol in chunks */
WHILE l_pos < l_blob_len LOOP
Dbms_Lob.Read(l_blob, l_amount, l_pos, l_buffer);
Utl_Smtp.write_raw_data(v_connection_handle,
Utl_Encode.BASE64_ENCODE(l_buffer));
Utl_Smtp.WRITE_DATA(v_connection_handle, Utl_Tcp.CRLF);
l_buffer := Null;
l_pos := l_pos + l_amount;
END LOOP;
Utl_Smtp.WRITE_DATA(v_connection_handle, Utl_Tcp.CRLF);
Dbms_Lob.FREETEMPORARY(l_blob);
Dbms_Lob.FILECLOSE(v_src_loc);
EXCEPTION
When Others Then
Dbms_Lob.FREETEMPORARY(l_blob);
Dbms_Lob.FILECLOSE(v_src_loc);
END;
END LOOP;
-- close email
Utl_Smtp.WRITE_DATA(v_connection_handle,
'--' || 'EMAIL_FILES.SECBOUND' || '--' || Utl_Tcp.CRLF);
Utl_Smtp.WRITE_DATA(v_connection_handle,
Utl_Tcp.CRLF || '.' || Utl_Tcp.CRLF);
END IF;
Utl_Smtp.CLOSE_DATA(v_connection_handle);
Utl_Smtp.QUIT(v_connection_handle);
EXCEPTION
When Others Then
-- utl_smtp.quit(v_connection_handle);
IF ifile Is Not Null Then
Utl_Smtp.QUIT(v_connection_handle);
END IF;
RAISE;
END;
您还可以查看我写的博客文章的以下链接,完整示例: