CREATE OR REPLACE PROCEDURE APPS.SEND_MAIL_HTML_TEST
is
v_message varchar2(2000);
conn utl_smtp.connection;
vrData RAW(32767);
sender varchar2(500) := 'prodmgr@test.ccc.com.tw';
receiver varchar2(500) :='keven@ccc.com.tw';
subj varchar2(200):='HTML Mail TEST';
is
v_message varchar2(2000);
conn utl_smtp.connection;
vrData RAW(32767);
sender varchar2(500) := 'prodmgr@test.ccc.com.tw';
receiver varchar2(500) :='keven@ccc.com.tw';
subj varchar2(200):='HTML Mail TEST';
--組成HTML語法
cursor cur1 is
select '<table>' aa from dual
union all
select '<tr><td><font size="20">' || segment1 || '</font> </td>'
end loop ;
cursor cur1 is
select '<table>' aa from dual
union all
select '<tr><td><font size="20">' || segment1 || '</font> </td>'
|| '<td><font size="20">' || description || '</font></td></tr>' aa
from mtl_system_items_b
where rownum<11
union all
select '</table>' aa from dual ;
r1 cur1%rowtype;
begin
from mtl_system_items_b
where rownum<11
union all
select '</table>' aa from dual ;
r1 cur1%rowtype;
begin
--指定SMTP SERVER
conn := utl_smtp.open_connection('192.168.1.10');
conn := utl_smtp.open_connection('192.168.1.10');
--也可以指定Port:utl_smtp.open_connection('192.168.1.10',25);
--對SMTP SERVER發出答應
utl_smtp.helo(conn, 'ccc.com.tw');
utl_smtp.helo(conn, 'ccc.com.tw');
--實際寄送的寄件者
utl_smtp.mail(conn, sender);
utl_smtp.mail(conn, sender);
--實際寄送的收件者
utl_smtp.rcpt(conn, receiver);
utl_smtp.open_data(conn);
--開啟傳送mail內容,先傳送header的定義,也是mail內容的一部份,用mail的原始檔查看可以得知。
UTL_smtp.write_data(conn, 'MIME-Version: ' || '1.0' ||UTL_tcp.CRLF);
UTL_smtp.write_data(conn, 'MIME-Version: ' || '1.0' ||UTL_tcp.CRLF);
--指定格式為HTML,並將語系設為utf-8
UTL_smtp.write_data(conn, 'Content-Type: ' || 'text/html; charset=utf-8' || UTL_tcp.CRLF);
UTL_smtp.write_data(conn, 'Content-Transfer-Encoding: ' || '8bit'|| UTL_tcp.CRLF);
UTL_smtp.write_data(conn, 'Content-Type: ' || 'text/html; charset=utf-8' || UTL_tcp.CRLF);
UTL_smtp.write_data(conn, 'Content-Transfer-Encoding: ' || '8bit'|| UTL_tcp.CRLF);
--如果格式為文字,則改為下列的語法,並將語系設為big5
--UTL_SMTP.WRITE_DATA(conn, 'Content-Type: text/plain; charset="big5"'||UTL_TCP.CRLF);
--UTL_SMTP.WRITE_DATA(conn, 'Content-Type: text/plain; charset="big5"'||UTL_TCP.CRLF);
--下面一樣是傳送mail的內容,寄件人、副件、密件,會自動將收件者依類型區分,
--實際上mail寄送是不管副件、密件…等,只管mail地址。
utl_smtp.write_data(conn, 'From: "Name" <'||sender||'>'|| UTL_tcp.CRLF);
utl_smtp.write_data(conn, 'To: "Name" <'||receiver||'>'|| UTL_tcp.CRLF);
-- utl_smtp.write_data(conn, 'Cc: "Name" <'||creceiver||'>'|| UTL_tcp.CRLF);
utl_smtp.write_data(conn, 'To: "Name" <'||receiver||'>'|| UTL_tcp.CRLF);
-- utl_smtp.write_data(conn, 'Cc: "Name" <'||creceiver||'>'|| UTL_tcp.CRLF);
--傳送mail的內容,主旨
UTL_SMTP.WRITE_RAW_DATA(conn, UTL_RAW.CAST_TO_RAW('Subject: '|| subj ||UTL_tcp.CRLF));
UTL_smtp.write_data(conn, UTL_tcp.CRLF);
for r1 in cur1 loop
UTL_SMTP.WRITE_RAW_DATA(conn, UTL_RAW.CAST_TO_RAW('Subject: '|| subj ||UTL_tcp.CRLF));
UTL_smtp.write_data(conn, UTL_tcp.CRLF);
for r1 in cur1 loop
--將要傳送的mail內文轉碼後傳送
vrData := utl_raw.cast_to_raw(r1.aa);
UTL_smtp.write_raw_data(conn, vrData);
vrData := utl_raw.cast_to_raw(r1.aa);
UTL_smtp.write_raw_data(conn, vrData);
end loop ;
--關閉連線
utl_smtp.close_data(conn);
utl_smtp.quit(conn);
EXCEPTION
WHEN UTL_smtp.transient_error OR UTL_smtp.permanent_error THEN
UTL_smtp.quit(conn);
WHEN OTHERS THEN
UTL_smtp.quit(conn);
utl_smtp.close_data(conn);
utl_smtp.quit(conn);
EXCEPTION
WHEN UTL_smtp.transient_error OR UTL_smtp.permanent_error THEN
UTL_smtp.quit(conn);
WHEN OTHERS THEN
UTL_smtp.quit(conn);
NUll;
end ;
END send_mail_html;
/
end ;
END send_mail_html;
/