Free Newsletters:
DatabaseDaily  
Database Journal
Search Database Journal:
 
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum DBA Videos
internet.com

» Database Journal Home
» DBA Videos
» Database Articles
» Database Tutorials
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» DBA Jobs
» Sitemap

News Via RSS Feed



follow us on Twitter

Marketplace Partners
Be a Marketplace Partner

internet.commerce
Be a Commerce Partner


















Mariposa Bot Shipped With Vodafone Smartphone

IT Job Market Heating Up: Report

Bing Makes Strides But Momentum Stalls

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Database Journal | DBA Support | SQLCourse | SQLCourse2







C++ Back Office Developer (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume

Featured Database Articles

Oracle

November 7, 2002

Sending E-mails from Oracle Database

Amar Kumar Padhi, amar_padhi@hotmail.com


Oracle 8i introduces an e-mail server option that allows you to send mail from within your code. So imagine firing e-mails to concerned persons when a critical parameter is altered, when a huge report is generated, when a night-job process fails, when a database has some issues (for DBAs), when an accounting process is completed, or when a month closing is over and so on. The list is infinite and varies from company to company on what level of alerts/notification and information sharing is to be maintained. What's more, if your local service provider supports SMS, you can even trigger an e-mail to a person's mobile.


What To Do

To get started, we'll need to make use of the UTL_SMTP and UTL_TCP packages. UTL_SMTP is the key package that liaises with the operating system protocol service to trigger the emails. You will have to write a customized routine to make use of these built-in packages and eventually call it in all applicable places in the system. E-mails can be triggered online as an event occurs or can be scheduled as a batch job (using the DBMS_JOB package).

I have created a package available for download here. Compile this package in the required database and call the anonymous block present at the end with the appropriate e-mail IDs. Once successfully tested, analyze how useful it can be to your product. You may also try the simple example given below.


What You Need

  1. Mail server IP name or IP address. This can be on the intranet or a remote available service on the internet. The word "localhost" can be used to tell Oracle to use the default Operating system SMTP service.

  2. Your OS should support SMTP. Almost all flavors of Unix support SMTP service. If on Windows 2000, check explicitly for SMT services. The word "localhost" as the mail server name should work. (My system crashed before I could try this, so this is in theory.)

  3. You need the port at which the SMTP service is listening. This is normally defaulted to 25 on most servers.

  4. The database should be Java enabled. The Jserver option must be installed using initjvm.sql and initplsj.sql and is necessary to be run. This is normally auto-installed in the Enterprise version.

  5. The UTL_SMTP package should exist in SYS user.


Key Routines in UTL_SMTP Package

UTL_SMTP.OPEN_CONNECTION: Opens a connection to the SMTP server.

UTL_SMTP.HELLO: Performs initial handshake.

UTL_SMTP.MAIL: Initiates a mail transaction with the server; the destination is a mailbox.

UTL_SMTP.RCPT: Specifies a recipient.

UTL_SMTP.DATA: Specifies the body.

UTL_SMTP.OPEN_DATA/UTL_SMTP.WRITE_DATA/UTL_SMTP.CLOSE_DATA: A finer way of specifying the body with more control.

UTL_SMTP.VRFY: Verifies the validity of an e-mail address.

UTL_SMTP.QUIT: Terminates the SMTP session and disconnects from the server.


Any Disadvantages?

I have come across one disadvantage while testing that isn't related to Oracle. The sender's e-mail ID can be framed, although this should be taken care of by the e-mail service in use. This option is enabled on most Unix servers. So it is up to the developer to instill stringent checks so that e-mails are not faked. E-mails can be sent with a default "system" ID if autogenerated; otherwise, e-mails needs to be pegged with a valid sender ID in order to maintain authenticity. There might be an option that can be set at the OS or e-mail server level, but I am not aware of one presently.

Care should also be taken to maintain a reasonable size for the e-mails. A large number of huge e-mails may overload the server. The size of the e-mail body would depend on what datatype is being used.


Simple example

declare
  l_maicon utl_smtp.connection;
begin
  l_maicon :=utl_smtp.open_connection('localhost');
  utl_smtp.helo(l_maicon,'hostname');
  utl_smtp.mail(l_maicon,'ts2017@emirates.com');
  utl_smtp.rcpt(l_maicon,'ts2017@emirates.com');
  utl_smtp.data(l_maicon,'From: ts2017@emirates.com' || utl_tcp.crlf||
                         'To: ts2017@emirates.com' || utl_tcp.crlf ||
                         'Subject: database e-mail option' || utl_tcp.crlf ||
                         'You have received this mail from database!');
  utl_smtp.quit(l_maicon);
end;


Common Errors

If the SMTP service is not available, the following error would occur:
ORA-20001: 421 Service not available

If Java is not installed properly you get the following error:
ORA-29540: class oracle/plsql/net/TCPConnection does not exist


» See All Articles by Columnist Amar Kumar Padhi




Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

Oracle Archives








Latest Forum Threads
Oracle Forum
Topic By Replies Updated
Free ERD program with import option ? tenamatt 1 February 26th, 01:39 PM
Record Select inet 1 February 26th, 01:38 PM
Rownum inet 1 February 18th, 10:01 AM
UPDATE statement performance sacrsv 2 January 19th, 07:58 PM









The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers