当前位置: 首页 > 图文教程 > 数据库 > Oracle > 使用utl_smtp从Oracle中发送电子邮件

Oracle
oracle sys_connect_by_path 函数 结果集连接
oracle join on 数据过滤问题
Oracle 当前用户下所有表的记录总数
oracle 树查询 语句
oracle 触发器 实现出入库
Oracle 函数大全
oracle 删除重复数据
ORACLE 最大连接数的问题
oracle 层次化查询(行政区划三级级联)
oracle 查询表名以及表的列名
Oracle 数据显示 横表转纵表
oracle 服务启动,关闭脚本(windows系统下)
ORCLE 表中列的修改
oracle 数据库连接分析
Oracle 实现类似SQL Server中自增字段的一个办法
Oracle 常用的SQL语句
Oracle 数组的学习 小知识也要积累,养成好的学习态度
Oracle 日期的一些简单使用
Oracle 数据库连接查询SQL语句
Oracle DBA常用语句

使用utl_smtp从Oracle中发送电子邮件


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-30   浏览: 98 ::
收藏到网摘: n/a

假如你目前运行的是Oracle10g或更高的版本,那么你现在就可以用新的utl_mail工具包从你的PL/SQL应用程序来发送电子邮件了。(注释:如果你的应用程序使构建在Oracle 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 ('[email protected]',
'[email protected]',
'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或段落标签来改变信息主体的格式。