在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的函数了~
【编辑推荐】