首先大概了解一下其中是做什么的,源码如下: /******************************************************************* * OVERVIEW * * This package provides SMTP client-side access functionality in PL/SQL. * With this package, a PL/SQL program can send electronic mails via SMTP. * This package does not allow the PL/SQL program to receive e-mails via * SMTP. The user of this package should be familiar with the SMTP protocol * as defined in RFC 821 and RFC 1869. * * This package is meant to provide an API to SMTP protocol directly. Users * may find it useful to define additional helper routines to encapsulate * the interaction with a SMTP server. * * USES * * A SMTP connection is initiated by a call to open_connection, which * returns a SMTP connection. After a connection is established, the * following calls are required to send a mail: * * helo() - identify the domain of the sender * mail() - start a mail, specify the sender * rcpt() - specify the recipient * open_data() - start the mail body * write_data() - write the mail body (multiple calls allowed) * close_data() - close the mail body and send the mail * * The SMTP connection is closed by calling quit(). * * A note on API style and raising PL/SQL exception: * * Most of the API has a function form and a procedure form. The function * form returns the reply message after the command is sent, in the form * of "XXX <an optional reply message>", where XXX is the reply code. * The procedure form of the same API calls the function form of the API, * checks the reply code and raises transient_error or permanent_error * exception if the reply code is in 400 or 500 range. The function form * of the API does not raise either of the 2 exceptions. * * All API may raise invalid_operation exception if it is called in either * of the situations: * * 1. calling API other than write_data(), write_raw_data() or close_data() * after open_data(0 is called, or * 2. calling write_data(), write_raw_data() or close_data() without * first calling open_data() * * EXAMPLES * Retrieve the home page from http://www.acme.com/ * * DECLARE * c utl_smtp.connection; * * PROCEDURE send_header(name IN VARCHAR2, header IN VARCHAR2) AS * BEGIN * utl_smtp.write_data(c, name || ': ' || header || utl_tcp.CRLF); * END; * * BEGIN * c := utl_smtp.open_connection('smtp-server.acme.com'); * utl_smtp.helo(c, 'foo.com'); * utl_smtp.mail(c, '[email protected]'); * utl_smtp.rcpt(c, '[email protected]'); * utl_smtp.open_data(c); * send_header('From', '"Sender" <[email protected]>'); * send_header('To', '"Recipient" <[email protected]>'); * send_header('Subject', 'Hello'); * utl_smtp.write_data(c, utl_tcp.CRLF || 'Hello, world!'); * utl_smtp.close_data(c); * utl_smtp.quit(c); * EXCEPTION * WHEN utl_smtp.transient_error OR utl_smtp.permanent_error THEN * BEGIN * utl_smtp.quit(c); * EXCEPTION * WHEN utl_smtp.transient_error OR utl_smtp.permanent_error THEN * NULL; -- When the SMTP server is down or unavailable, we don't * -- have a connection to the server. The quit call will * -- raise an exception that we can ignore. * END; * raise_application_error(-20000, * 'Failed to send mail due to the following error: ' || sqlerrm); * END; */ /* * SMTP connection type */ 然后,试着调用包里的方法 最后,到网上找些例子练习练习
/*******************************************************************
* OVERVIEW
*
* This package provides SMTP client-side access functionality in PL/SQL.
* With this package, a PL/SQL program can send electronic mails via SMTP.
* This package does not allow the PL/SQL program to receive e-mails via
* SMTP. The user of this package should be familiar with the SMTP protocol
* as defined in RFC 821 and RFC 1869.
*
* This package is meant to provide an API to SMTP protocol directly. Users
* may find it useful to define additional helper routines to encapsulate
* the interaction with a SMTP server.
*
* USES
*
* A SMTP connection is initiated by a call to open_connection, which
* returns a SMTP connection. After a connection is established, the
* following calls are required to send a mail:
*
* helo() - identify the domain of the sender
* mail() - start a mail, specify the sender
* rcpt() - specify the recipient
* open_data() - start the mail body
* write_data() - write the mail body (multiple calls allowed)
* close_data() - close the mail body and send the mail
*
* The SMTP connection is closed by calling quit().
*
* A note on API style and raising PL/SQL exception:
*
* Most of the API has a function form and a procedure form. The function
* form returns the reply message after the command is sent, in the form
* of "XXX <an optional reply message>", where XXX is the reply code.
* The procedure form of the same API calls the function form of the API,
* checks the reply code and raises transient_error or permanent_error
* exception if the reply code is in 400 or 500 range. The function form
* of the API does not raise either of the 2 exceptions.
*
* All API may raise invalid_operation exception if it is called in either
* of the situations:
*
* 1. calling API other than write_data(), write_raw_data() or close_data()
* after open_data(0 is called, or
* 2. calling write_data(), write_raw_data() or close_data() without
* first calling open_data()
*
* EXAMPLES
* Retrieve the home page from http://www.acme.com/
*
* DECLARE
* c utl_smtp.connection;
*
* PROCEDURE send_header(name IN VARCHAR2, header IN VARCHAR2) AS
* BEGIN
* utl_smtp.write_data(c, name || ': ' || header || utl_tcp.CRLF);
* END;
*
* BEGIN
* c := utl_smtp.open_connection('smtp-server.acme.com');
* utl_smtp.helo(c, 'foo.com');
* utl_smtp.mail(c, '[email protected]');
* utl_smtp.rcpt(c, '[email protected]');
* utl_smtp.open_data(c);
* send_header('From', '"Sender" <[email protected]>');
* send_header('To', '"Recipient" <[email protected]>');
* send_header('Subject', 'Hello');
* utl_smtp.write_data(c, utl_tcp.CRLF || 'Hello, world!');
* utl_smtp.close_data(c);
* utl_smtp.quit(c);
* EXCEPTION
* WHEN utl_smtp.transient_error OR utl_smtp.permanent_error THEN
* BEGIN
* utl_smtp.quit(c);
* EXCEPTION
* WHEN utl_smtp.transient_error OR utl_smtp.permanent_error THEN
* NULL; -- When the SMTP server is down or unavailable, we don't
* -- have a connection to the server. The quit call will
* -- raise an exception that we can ignore.
* END;
* raise_application_error(-20000,
* 'Failed to send mail due to the following error: ' || sqlerrm);
* END;
*/ /*
* SMTP connection type
*/
然后,试着调用包里的方法
最后,到网上找些例子练习练习