利用oracle存储过程来发送邮件
2008-09-01 12:46:14 来源:WEB开发网3.创建SendMail过程这里主要高用utl_smtp这个包
CREATEORREPLACEPROCEDURESEND_MAIL
(as_senderinvarchar2,--邮件发送者
as_recpinvarchar2,--邮件接收者
as_subjectinvarchar2,--邮件标题
as_msg_bodyinvarchar2)--邮件内容
IS
ls_mailhostvarchar2(30):='***.***.***.***';--addressorIP
lc_mail_connutl_smtp.connection;
ls_subjectvarchar2(100);
ls_msg_bodyvarchar2(20000);
ls_usernamevarchar2(256):='zhangxf';
ls_passwordvarchar2(256):='libozxf';
BEGIN
lc_mail_conn:=utl_smtp.open_connection(ls_mailhost,25);
utl_smtp.helo(lc_mail_conn,ls_mailhost);
utl_smtp.command(lc_mail_conn,'AUTHLOGIN');
utl_smtp.command(lc_mail_conn,demo_base64.encode(utl_raw.cast_to_raw(ls_username)));
utl_smtp.command(lc_mail_conn,demo_base64.encode(utl_raw.cast_to_raw(ls_password)));
ls_subject:='Subject:['||upper(sys_context('userenv','db_name'))||']-'||as_subject;
ls_msg_body:=as_msg_body;
utl_smtp.mail(lc_mail_conn,'<'||as_sender||'>');--这里的'<'一定要写,不然会出现permanenterror
utl_smtp.rcpt(lc_mail_conn,'<'||as_recp||'>');--这里的'<'一定要写,不然会出现permanenterror
utl_smtp.open_data(lc_mail_conn);
ls_msg_body:='From:'||as_sender||chr(13)||chr(10)||'To:'||as_recp||chr(13)||chr(10)||ls_subject||
chr(13)||chr(10)||chr(13)||chr(10)||ls_msg_body;
utl_smtp.write_raw_data(lc_mail_conn,utl_raw.cast_to_raw(ls_msg_body));--这样写subject可以支持中文但body内容不支持中文;
--utl_smtp.write_data(lc_mail_conn,ls_msg_body);--这样写subject不支持中文
utl_smtp.close_data(lc_mail_conn);
utl_smtp.quit(lc_mail_conn);
EXCEPTION
WHENUTL_SMTP.INVALID_OPERATIONTHEN
dbms_output.put_line('invalidoperation');
WHENUTL_SMTP.TRANSIENT_ERRORTHEN
dbms_output.put_line('transienterror');
WHENUTL_SMTP.PERMANENT_ERRORTHEN
dbms_output.put_line('permanenterror');
WHENOTHERSTHEN
dbms_output.put_line('others');
endsend_mail;
4.测试
execsend_mail('zxf_feng@f163.com','zxf_feng@163.com','test','test');
赞助商链接