Pada paket UTL_SMTP mulai diperkenalkan pada versi Oracle 8i sehingga dapat digunakan untuk mengirim email dari PL/SQL. Paket UTL_SMTP membutuhkan JServer yang dapat diinstal dengan menjalankan skrip berikut sebagai SYS.
SQL> @$ORACLE_HOME/javavm/install/initjvm.sql
SQL> @$ORACLE_HOME/rdbms/admin/initplsj.sql
Contoh membuat email dengan beberapa penerima dengan Oracle :
CREATE OR REPLACE PROCEDURE send_mail (p_to IN VARCHAR2,
p_cc IN VARCHAR2 DEFAULT NULL,p_bcc IN VARCHAR2 DEFAULT NULL,p_from IN VARCHAR2,p_subject IN VARCHAR2,p_message IN VARCHAR2,p_smtp_host IN VARCHAR2,p_smtp_port IN NUMBER DEFAULT 25)
AS
l_mail_conn UTL_SMTP.connection;
PROCEDURE process_recipients(p_mail_conn IN OUT UTL_SMTP.connection,
p_list IN VARCHAR2)
AS
l_tab string_api.t_split_array;
BEGIN
IF TRIM(p_list) IS NOT NULL THEN
l_tab := string_api.split_text(p_list);
FOR i IN 1 .. l_tab.COUNT LOOP
UTL_SMTP.rcpt(p_mail_conn, TRIM(l_tab(i)));
END LOOP;
END IF;
END;
BEGIN
l_mail_conn := UTL_SMTP.open_connection(p_smtp_host, p_smtp_port);
UTL_SMTP.helo(l_mail_conn, p_smtp_host);
UTL_SMTP.mail(l_mail_conn, p_from);
process_recipients(l_mail_conn, p_to);
process_recipients(l_mail_conn, p_cc);
process_recipients(l_mail_conn, p_bcc);
UTL_SMTP.open_data(l_mail_conn);
UTL_SMTP.write_data(l_mail_conn, 'Date: ' || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'To: ' || p_to || UTL_TCP.crlf);
IF TRIM(p_cc) IS NOT NULL THEN
UTL_SMTP.write_data(l_mail_conn, 'CC: ' || REPLACE(p_cc, ',', ';') || UTL_TCP.crlf);
END IF;
IF TRIM(p_bcc) IS NOT NULL THEN
UTL_SMTP.write_data(l_mail_conn, 'BCC: ' || REPLACE(p_bcc, ',', ';') || UTL_TCP.crlf);
END IF;
UTL_SMTP.write_data(l_mail_conn, 'From: ' || p_from || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Subject: ' || p_subject || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Reply-To: ' || p_from || UTL_TCP.crlf || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, p_message || UTL_TCP.crlf || UTL_TCP.crlf);
UTL_SMTP.close_data(l_mail_conn);
UTL_SMTP.quit(l_mail_conn);
END;
/
Cara memanggil :
BEGIN
send_mail(p_to => 'me@ptxzy.com',
p_cc => 'rekan1@ptxzy.com',
p_bcc => 'bos@ptxzy.com',
p_from => 'admin@ptxzy.com',
p_subject => 'Test Message',
p_message => 'This is a test message.',
p_smtp_host => 'smtp.ptxzy.com');
END;
/