频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

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

来源:pcdog 作者:佚名 出处:巧巧读书 2007-12-18 进入讨论组

  假如你目前运行的是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 ('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或段落标签来改变信息主体的格式。

巧 巧 读 书:http://www.qqread.com/oracle/2007/12/u387106.html 更多文章 更多内容请看电子邮件安全Oracle 10g基础应用邮件服务器专题专题,或进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章