Send authenticated email using UTL_SMTP
Autor: Denilson Nascimento | Publicado: 2013-06-24T11:34:00-03:00


CREATE OR REPLACE PROCEDURE send_mail (p_to &nbsp; &nbsp; &nbsp; &nbsp;IN VARCHAR2,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p_from &nbsp; &nbsp; &nbsp;IN VARCHAR2,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p_subject &nbsp; IN VARCHAR2,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p_text_msg &nbsp;IN VARCHAR2 DEFAULT NULL,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p_html_msg &nbsp;IN VARCHAR2 DEFAULT NULL,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p_smtp_host IN VARCHAR2,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p_smtp_port IN NUMBER DEFAULT 587)
AS
&nbsp; l_mail_conn &nbsp; UTL_SMTP.connection;
&nbsp; l_boundary &nbsp; &nbsp;VARCHAR2(50) := '----=*#abc1234321cba#*=';
BEGIN


-- Autentication Configuration
l_mail_conn := UTL_SMTP.open_connection(p_smtp_host, p_smtp_port);
utl_smtp.command(l_mail_conn,'AUTH LOGIN');
utl_smtp.command(l_mail_conn,utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw('denilson@denilson.com.br'))));
utl_smtp.command(l_mail_conn,utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw('password'))));

&nbsp; UTL_SMTP.helo(l_mail_conn, p_smtp_host);
&nbsp; UTL_SMTP.mail(l_mail_conn, p_from);
&nbsp; UTL_SMTP.rcpt(l_mail_conn, p_to);
&nbsp; UTL_SMTP.open_data(l_mail_conn);
&nbsp; 
&nbsp; UTL_SMTP.write_data(l_mail_conn, 'Date: ' || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') || UTL_TCP.crlf);
&nbsp; UTL_SMTP.write_data(l_mail_conn, 'To: ' || p_to || UTL_TCP.crlf);
&nbsp; UTL_SMTP.write_data(l_mail_conn, 'From: ' || p_from || UTL_TCP.crlf);
&nbsp; UTL_SMTP.write_data(l_mail_conn, 'Subject: ' || p_subject || UTL_TCP.crlf);
&nbsp; UTL_SMTP.write_data(l_mail_conn, 'Reply-To: ' || p_from || UTL_TCP.crlf);
&nbsp; UTL_SMTP.write_data(l_mail_conn, 'MIME-Version: 1.0' || UTL_TCP.crlf);
&nbsp; UTL_SMTP.write_data(l_mail_conn, 'Content-Type: multipart/alternative; boundary="' || l_boundary || '"' || UTL_TCP.crlf || UTL_TCP.crlf);

&nbsp; IF p_text_msg IS NOT NULL THEN
&nbsp; &nbsp; UTL_SMTP.write_data(l_mail_conn, '--' || l_boundary || UTL_TCP.crlf);
&nbsp; &nbsp; UTL_SMTP.write_data(l_mail_conn, 'Content-Type: text/plain; charset="iso-8859-1"' || UTL_TCP.crlf || UTL_TCP.crlf);
&nbsp; &nbsp; UTL_SMTP.write_data(l_mail_conn, p_text_msg);
&nbsp; &nbsp; UTL_SMTP.write_data(l_mail_conn, UTL_TCP.crlf || UTL_TCP.crlf);
&nbsp; END IF;

&nbsp; IF p_html_msg IS NOT NULL THEN
&nbsp; &nbsp; UTL_SMTP.write_data(l_mail_conn, '--' || l_boundary || UTL_TCP.crlf);
&nbsp; &nbsp; UTL_SMTP.write_data(l_mail_conn, 'Content-Type: text/html; charset="iso-8859-1"' || UTL_TCP.crlf || UTL_TCP.crlf);
&nbsp; &nbsp; UTL_SMTP.write_data(l_mail_conn, p_html_msg);
&nbsp; &nbsp; UTL_SMTP.write_data(l_mail_conn, UTL_TCP.crlf || UTL_TCP.crlf);
&nbsp; END IF;

&nbsp; UTL_SMTP.write_data(l_mail_conn, '--' || l_boundary || '--' || UTL_TCP.crlf);
&nbsp; UTL_SMTP.close_data(l_mail_conn);
&nbsp; UTL_SMTP.quit(l_mail_conn);
END;
/

Calling Procedure

DECLARE
&nbsp; l_html VARCHAR2(32767);
BEGIN
&nbsp; &nbsp; &nbsp;l_html := '&lt;html&gt;
&nbsp; &nbsp; &lt;head&gt;
&nbsp; &nbsp; &nbsp; &lt;title&gt;Test HTML message&lt;/title&gt;
&nbsp; &nbsp; &lt;/head&gt;
&nbsp; &nbsp; &lt;body&gt;
&nbsp; &nbsp; &nbsp; &lt;p&gt;TESTE DE ENVIO DE EMAIL UTILIZANDO SMTP AUTENTICADO&lt;/p&gt;
&nbsp; &nbsp; &nbsp; &lt;p&gt;&lt;a href="http://dicasoracledba.blogspot.com" /&gt; Dicas Oracle DBA &lt;/a&gt;
&nbsp; &nbsp; &lt;/body&gt;
&nbsp; &lt;/html&gt;';

&nbsp; send_mail(p_to &nbsp; &nbsp; &nbsp; &nbsp;=&gt; 'denilson@denilson.com.br',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p_from &nbsp; &nbsp; &nbsp;=&gt; 'denilson@denilson.com.br',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p_subject &nbsp; =&gt; 'teste',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p_text_msg &nbsp;=&gt; l_html,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p_html_msg &nbsp;=&gt; l_html,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p_smtp_host =&gt; 'smtp.denilson.com.br',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p_smtp_port =&gt; '587' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );
END;
/