在Procedure中p_receiver為收件人地址,多收件人以“,”(英文逗號)分割,
p_sub為郵件標題,p_txt為郵件內容(支持Html)。代碼如下:
- createorreplaceprocedurebsd_sendemail(p_receivervarchar2,p_subvarchar2,p_txtvarchar2)
- is
- p_uservarchar2(30):='';
- p_passvarchar2(30):='';
- p_sendorvarchar2(20):='GnIE';
- p_servervarchar2(20):='Mail_Server_IP';
- p_portnumber:=25;
- p_need_smtpnumber:=0;
- p_subjectvarchar2(4000);
- l_crlfvarchar2(2):=utl_tcp.crlf;
- l_sendoraddressvarchar2(4000);
- l_splitevarchar2(10):='++';
- boundaryconstantvarchar2(256):='-----BYSUK';
- first_boundaryconstantvarchar2(256):='--'||boundary||l_crlf;
- last_boundaryconstantvarchar2(256):='--'||boundary||'--'||l_crlf;
- multipart_mime_typeconstantvarchar2(256):='multipart/mixed;boundary="'||boundary||'"';
- typeaddress_lististableofvarchar2(100)indexbybinary_integer;
- my_address_listaddress_list;
- ---------------------------------------分割郵件地址----------------------------------------------
- procedurep_splite_str(p_strvarchar2,p_splite_flagintdefault1)is
- l_addrvarchar2(254):='';
- l_lenint;
- l_strvarchar2(4000);
- jint:=0;--表示郵件地址或者附件的個數
- begin
- /*處理接收郵件地址列表,包括去空格、將;轉換為,等*/
- l_str:=trim(rtrim(replace(replace(p_str,';',','),'',''),','));
- l_len:=length(l_str);
- foriin1..l_lenloop
- ifsubstr(l_str,i,1)<>','then
- l_addr:=l_addr||substr(l_str,i,1);
- else
- j:=j+1;
- ifp_splite_flag=1then--表示處理郵件地址
- --前後需要加上'<>',否則很多郵箱將不能發送郵件
- l_addr:='<'||l_addr||'>';
- --調用郵件發送過程
- my_address_list(j):=l_addr;
- endif;
- l_addr:='';
- endif;
- ifi=l_lenthen
- j:=j+1;
- ifp_splite_flag=1then
- --調用郵件發送過程
- l_addr:='<'||l_addr||'>';
- my_address_list(j):=l_addr;
- endif;
- endif;
- endloop;
- end;
- ------------------------------------------------寫郵件頭和郵件內容------------------------------------------
- procedurewrite_data(p_conninoutnocopyutl_smtp.connection,
- p_nameinvarchar2,
- p_valueinvarchar2,
- p_splitevarchar2default':',
- p_crlfvarchar2defaultl_crlf)is
- begin
- /*utl_raw.cast_to_raw對解決中文亂碼問題很重要*/
- utl_smtp.write_raw_data(p_conn,utl_raw.cast_to_raw(convert(p_name||p_splite||p_value||p_crlf,'ZHS16CGB231280')));
- end;
- ----------------------------------------寫mime郵件尾部-----------------------------------------------------
- procedureend_boundary(conninoutnocopyutl_smtp.connection,lastinbooleandefaultfalse)is
- begin
- utl_smtp.write_data(conn,utl_tcp.crlf);
- if(last)then
- utl_smtp.write_data(conn,last_boundary);
- endif;
- end;
- ---------------------------------------------真正發送郵件的過程--------------------------------------------
- procedurep_email(p_sendoraddress2varchar2,--發送地址
- p_receiveraddress2varchar2)--接受地址
- is
- l_connutl_smtp.connection;--定義連接
- begin
- /*初始化郵件服務器信息,連接郵件服務器*/
- l_conn:=utl_smtp.open_connection(p_server,p_port);
- utl_smtp.helo(l_conn,p_server);
- /*smtp服務器登錄校驗*/
- ifp_need_smtp=1then
- utl_smtp.command(l_conn,'AUTHLOGIN','');
- utl_smtp.command(l_conn,utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw(p_user))));
- utl_smtp.command(l_conn,utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw(p_pass))));
- endif;
- /*設置發送地址和接收地址*/
- utl_smtp.mail(l_conn,p_sendoraddress2);
- utl_smtp.rcpt(l_conn,p_receiveraddress2);
- /*設置郵件頭*/
- utl_smtp.open_data(l_conn);
- /*設置日期*/
- --write_data(l_conn,'Date',to_char(sysdate-1/3,'ddMonyyhh24:mi:ss'));
- /*設置發送人*/
- write_data(l_conn,'From',p_sendor);
- /*設置接收人*/
- write_data(l_conn,'To',p_receiver);
- /*設置郵件主題*/
- selectreplace('=?GB2312?B?'||utl_raw.cast_to_varchar2(utl_encode.base64_encode(rawtohex(p_sub)))||'?=',utl_tcp.crlf,'')intop_subjectfromdual;
- write_data(l_conn,'Subject',p_subject);
- write_data(l_conn,'Content-Type',multipart_mime_type);
- utl_smtp.write_data(l_conn,utl_tcp.crlf);
- utl_smtp.write_data(l_conn,first_boundary);
- write_data(l_conn,'Content-Type','text/Html;charset=gb2312');
- --單獨空一行,否則,正文內容不顯示
- utl_smtp.write_data(l_conn,utl_tcp.crlf);
- /*設置郵件正文
- 把分隔符還原成chr(10)。這主要是為了shell中調用該過程,如果有多行,則先把多行的內容合並成一行,並用l_splite分隔
- 然後用l_crlf替換chr(10)。這一步是必須的,否則將不能發送郵件正文有多行的郵件
- */
- write_data(l_conn,'',replace(replace(p_txt,l_splite,chr(10)),chr(10),l_crlf),'','');
- end_boundary(l_conn);
- /*關閉數據寫入*/
- utl_smtp.close_data(l_conn);
- /*關閉連接*/
- utl_smtp.quit(l_conn);
- end;
- ---------------------------------------------主過程-----------------------------------------------------
- begin
- l_sendoraddress:='<'||p_sendor||'>';
- p_splite_str(p_receiver);--處理郵件地址
- forkin1..my_address_list.countloop
- p_email(l_sendoraddress,my_address_list(k));
- endloop;
- /*處理郵件地址,根據逗號分割郵件*/
- end;
在PL/SQL中測試一下它的性能如何:
到Outlook中看看Oracle發送Email郵件到了沒有:
哈哈!及時收到,通過它可以在底層發送相關數據信息,希望對大家有用!
另,不知道11g有沒有直接用Oracle發送Email的函數了~