发送邮件的存储过程
CREATE OR REPLACE PROCEDURE Up_send_mail(subject in varchar2(500),mail_content in varchar2(2000),re_out out number(2))
AS
smtp_conn utl_smtp.connection;
--定义发邮件用户名
user_name varchar2(50) := utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw('mailname@163.com')));
--定义发邮件的密码
user_paswd varchar2(50) := utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw('mailpass')));
--定义邮件表头
lv_mail_header varchar2(200):='From:mailname@163.com'||utl_tcp.CRLF||
'To:tomail@qq.com'||utl_tcp.CRLF||
'Subject:'||subject||utl_tcp.CRLF;
--定义邮件正文
lv_mail_content varchar2(2000):=utl_tcp.CRLF||mail_content ;
begin
re_out :=0;
smtp_conn := utl_smtp.open_connection('smtp.163.com',25);
utl_smtp.helo(smtp_conn,'smtp.163.com');
utl_smtp.command(smtp_conn,'AUTH LOGIN');
utl_smtp.command(smtp_conn,user_name);
utl_smtp.command(smtp_conn,user_paswd);
utl_smtp.mail(smtp_conn,'<mailname@163.com>');
utl_smtp.rcpt(smtp_conn,'<tomail@qq.com>');
utl_smtp.open_data(smtp_conn);
utl_smtp.write_data(smtp_conn,lv_mail_header);
utl_smtp.write_raw_data(smtp_conn,utl_raw.cast_to_raw(lv_mail_content));
utl_smtp.close_data(smtp_conn);
utl_smtp.quit(smtp_conn);
exception
when others then
re_out := 1;
utl_smtp.quit(smtp_conn);
RETURN;
end;
注:
mailname@163.com 改成发送邮箱的地址
mailpass 改成发送邮箱的密码
tomail@qq.com 改成接收邮箱的地址
调用存储过程
DECLARE
subject_in varchar(500) := 'Oracle Tablespace mail';
mail_content_in varchar(2000):='这是一个测试邮件,不知道是做什么用的!';
re_out_out number(2);
BEGIN
Up_send_mail(subject_in,mail_content_in,re_out_out );
IF re_out_out=0
THEN
DBMS_OUTPUT.put_line('发送成功');
ELSE
DBMS_OUTPUT.put_line('发送失败');
END IF;
END;