快捷搜索:

通过Oracle发送Email

本篇是经由过程Oracle10g发送Email实例,该Procedure支持中文邮件、HTML代码、多收件人。

在Procedure中p_receiver为收件人地址,多收件人以“,”(英文逗号)瓜分,

p_sub为邮件标题,p_txt为邮件内容(支持HTML)。代码如下:

create or replace procedure bsd_sendemail (p_receiver varchar2,p_sub varchar2,p_txt varchar2)

is

p_user varchar2(30) := '';

p_pass varchar2(30) := '';

p_sendor varchar2(20) := 'Gnie';

p_server varchar2(20) := 'Mail_Server_IP';

p_port number := 25;

p_need_smtp number := 0;

p_subject varchar2(4000);

l_crlf varchar2(2) := utl_tcp.crlf;

l_sendoraddress varchar2(4000);

l_splitevarchar2(10) := '++';

boundaryconstant varchar2(256) := '-----BYSUK';

first_boundaryconstant varchar2(256) := '--' || boundary || l_crlf;

last_boundaryconstant varchar2(256) := '--' || boundary || '--' || l_crlf;

multipart_mime_type constant varchar2(256) := 'multipart/mixed; boundary="' || boundary || '"';

type address_list is table of varchar2(100) index by binary_integer;

my_address_list address_list;

---------------------------------------瓜分邮件地址----------------------------------------------

procedure p_splite_str(p_str varchar2,p_splite_flag int default 1) is

l_addr varchar2(254) := '';

l_lenint;

l_strvarchar2(4000);

jint := 0; --表示邮件地址或者附件的个数

begin

/*处置惩罚接管邮件地址列表,包括去空格、将;转换为,等*/

l_str := trim(rtrim(replace(replace(p_str, ';', ','), ' ', ''), ','));

l_len := length(l_str);

for i in 1 .. l_len loop

if substr(l_str, i, 1) ',' then

l_addr := l_addr || substr(l_str, i, 1);

else

j := j + 1;

if p_splite_flag = 1 then --表示处置惩罚邮件地址

--前后必要加上'l_addr := '|| l_addr || '>';

--调用邮件发送历程

my_address_list(j) := l_addr;

end if;

l_addr := '';

end if;

if i = l_len then

j := j + 1;

if p_splite_flag = 1 then

--调用邮件发送历程

l_addr := '|| l_addr || '>';

my_address_list(j) := l_addr;

end if;

end if;

end loop;

end;

------------------------------------------------写邮件头和邮件内容------------------------------------------

procedure write_data(p_connin out nocopy utl_smtp.connection,

p_namein varchar2,

p_valuein varchar2,

p_splite varchar2 default ':',

p_crlfvarchar2 default l_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邮件尾部-----------------------------------------------------

procedure end_boundary(conn in out nocopy utl_smtp.connection, last in boolean default false) is

begin

utl_smtp.write_data(conn, utl_tcp.crlf);

if (last) then

utl_smtp.write_data(conn, last_boundary);

end if;

end;

---------------------------------------------真正发送邮件的历程--------------------------------------------

procedure p_email(p_sendoraddress2 varchar2, --发送地址

p_receiveraddress2 varchar2) --吸收地址

is

l_conn utl_smtp.connection; --定义连接

begin

/*初始化邮件办事器信息,连接邮件办事器*/

l_conn := utl_smtp.open_connection(p_server, p_port);

utl_smtp.helo(l_conn, p_server);

/* smtp办事器登录校验 */

if p_need_smtp = 1 then

utl_smtp.command(l_conn, 'AUTH LOGIN', '');

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))));

end if;

/*设置发送地址和接管地址*/

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, 'dd Mon yy hh24:mi:ss'));

/*设置发送人*/

write_data(l_conn, 'From', p_sendor);

/*设置接管人*/

write_data(l_conn, 'To', p_receiver);

/*设置邮件主题*/

select replace('=?GB2312?B?'||utl_raw.cast_to_varchar2(utl_encode.base64_encode(rawtohex(p_sub)))||'?=',utl_tcp.crlf,'')

into p_subject from dual;

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);--处置惩罚邮件地址

for k in 1 .. my_address_list.count loop

p_email(l_sendoraddress, my_address_list(k));

end loop;

/*处置惩罚邮件地址,根据逗号瓜分邮件*/

end;

在PL/SQL中测试一下它的机能若何:

到Outlook中看看邮件到了没有:

哈哈!及时收到,经由过程它可以在底层发送相关数据信息,盼望对大年夜家有用!

另,不知道11g有没有直接发送Mail的函数了~

::源代码下载::

转自:http://www.cnblogs.com/gnielee/archive/2009/08/06/1540578.html

您可能还会对下面的文章感兴趣: