oracle数据库发送邮件功能,利用utl_smtp从Oracle数据库发送电子邮件

如果你运行的是10g或更高的版本,你就可以新的utl_mail工具包从你的PL/SQL应用程序发送电子邮件。即使你的应用程序构建在 8i数据库的基础上,却运行维护在Oracle 10g或以上版本,也可以通过旧的utl_smtp工具包来发送电子邮件(只是操作更为复杂一点)。

utl_smtp代码的一个优点就是可以在Oracle 10g上正常运行,所以我们不需要用utl_mail来替代utl_smtp工具包。虽然utl_mail完全替代utl_smtp的一天勿庸置疑会降临,不过现在utl_smtp还能够满足我们的需求。

首先,确认utl_smtp工具包已经安装在你的系统里(当然是在SYS架构里)。如果你还没有安装这个工具包,可以在你的ORACLE_HOME\RDBMS\admin文件夹中找到utlsmtp.sql脚本。你还需要utl_tcp包;同样的,如果你发现utl_tcp包还没有加载,也可以从跟utlsmtp.sql脚本相同的路径找到utltcp.sql脚本。最后,你还需要知道你的企业SMTP的URL.(注意,下面的例子不适用于对SMTP进行了设置的,如Gmail)

这个小小的程序包说明语句非常简单明了:

以下是引用片段:  create or replace PACKAGE sendmail IS

procedure send (p_sender varchar2,

p_recipient varchar2,

p_subject varchar2,

p_body varchar2 default null);

end sendmail;

观察以上语句的主体和下面的程序包,你会发现send的公共模式(public method)依赖于被称为common的私有模式(private method),这样以后我们可以对这个程序包进行扩展,并为大家展示怎样发送二进制大对象(blob)附件。例如,如果你生成了一份PDF文档,并把它保存在你的数据库里,你可以会想要把它作为电子邮件的附件发送出去, common模式就是用来完成这个任务的。将要用到的代码来自于基本send模式和send_blob模式。

以下就是程序包的主体:

以下是引用片段:  create or replace PACKAGE BODY sendmail IS

procedure common (p_sender varchar2,

p_recipient varchar2,

p_subject varchar2,

c out utl_smtp.connection) is

v_recipient varchar2(1000);

begin

--make connection to smtp

c := utl_smtp.open_connection('smtp.example.com');

--identify the domain of the sender

utl_smtp.helo(c, 'example.com');

--start a mail, specify the sender

utl_smtp.mail(c, p_sender);

--identify recipient

utl_smtp.rcpt(c, v_recipient);

--start the mail body

utl_smtp.open_data(c);

utl_smtp.write_data(c, 'From: ' || p_sender || utl_tcp.crlf);

utl_smtp.write_data(c, 'To: ' || p_recipient || utl_tcp.crlf);

utl_smtp.write_data(c, 'Subject: ' || p_subject || utl_tcp.crlf);

exception

when utl_smtp.transient_error or utl_smtp.permanent_error then

utl_smtp.quit(c);

raise;

when others then

raise;

end common;

procedure send (p_sender varchar2,

p_recipient varchar2,

p_subject varchar2,

p_body varchar2 default null) is

c utl_smtp.connection;

begin

common(p_sender, p_recipient, p_subject, c);

utl_smtp.write_data(c, 'Content-Type: text/html' || utl_tcp.crlf);

utl_smtp.write_data(c, utl_tcp.crlf || p_body);

utl_smtp.close_data(c);

utl_smtp.quit(c);

exception

when utl_smtp.transient_error or utl_smtp.permanent_error then

utl_smtp.quit(c);

raise;

when others then

raise;

end send;

end sendmail;

上面的程序里有些地方是需要用具体的信息来替代的。第一个是提供SMTP服务器的地方,需要添加你自己要添加的任何企业STMP服务器:

——make connection to smtp

c := utl_smtp.open_connection('smtp.example.com');

其次是需要验证域的地方,需要用你的确切域名来替代:

——identify the domain of the sender

utl_smtp.helo(c, 'example.com');

上面就是获取基本的电子邮件功能所需要的所有程序,你可以通过以下的语句调用这个程序:

以下是引用片段:  begin

sendmail.send ('sender@example.com',

'recipient@example.com',

'Subject: Hello',

'How about your trip?');

end;

你会发现上面在邮件的主体部分(body)的字符串嵌入了超文本编辑模式效果(字体为粗体)。这是因为前面在send模式下将内容类型设置为text/html:

utl_smtp.write_data(c, 'Content-Type: text/html' || utl_tcp.crlf);

不过,不要浪费时间想要把chr(10)作为换行符嵌入到带格式的电子邮件中发送出去,这样行不通的,因为HTML模式会把它忽略掉。你必须使用break或段落标签来改变信息主体的格式。

以后,我们还会讲述如何将blob数据作为附件发送出去。

【责编:John】

--------------------next---------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值