La Web del Programador: Comunidad de Programadores
 
    Pregunta:  41950 - ENVIAR CORREO DESDE ORACLE
Autor:  Alfonso Aguinaga
Mi intenciĆ³n es enviar desde un formulario de Developer o desde un procedimiento de SQL Plus un correo electronico pero no se cual es el modo de hacerlo.

Gracias.

  Respuesta:  No No
Si quieres adjuntar un blob puedes hacerlo con un procedimiento como este...

PROCEDURE PR_Enviar_Correo_Con_Adjunto(PI_REMITENTE IN VARCHAR2,
PI_DESTINATARIO IN VARCHAR2,
PI_ASUNTO IN VARCHAR2,
PI_CUERPO IN VARCHAR2,
PI_C_CORREO_ENVIADO IN NUMBER,
PI_DOCADJ IN BLOB,
PI_TDOCADJ IN VARCHAR2) IS
V_Crlf VARCHAR2(2) := UTL_TCP.CRLF;
V_Conexion UTL_SMTP.CONNECTION;
V_Cabecera VARCHAR2(1000);
nPos NUMBER;
cadena VARCHAR2(1000);
-- Codigo anyadido para adjuntar correo
msg varchar2(32767);
v_raw raw(57);
v_length integer := 0;
v_buffer_size integer := 57;
v_offset integer := 1;
-- FIN Codigo anyadido para adjuntar correo
BEGIN

--Se establece la conexion al servidor de correo
V_Conexion := UTL_SMTP.OPEN_CONNECTION(C_SMTP_SERVER,25);

--Se construye la cabecera del mensaje
msg := 'Return-Path: '||PI_REMITENTE|| utl_tcp.CRLF ||
'Sent: '||TO_CHAR( SYSDATE, 'dd Mon yy hh24:mi:ss' )|| utl_tcp.CRLF ||
'From: '||PI_REMITENTE|| utl_tcp.CRLF ||
'Subject: '|| PI_ASUNTO || utl_tcp.CRLF ||
'To: '|| PI_DESTINATARIO || utl_tcp.CRLF ||
'Cc: '|| NULL || utl_tcp.CRLF ||
'MIME-Version: 1.0'|| utl_tcp.CRLF || -- Use MIME mail standard
'Content-Type: multipart/mixed; boundary="MIME.Bound"'|| utl_tcp.CRLF || --MIME.Bound really should be a randomly generated string
utl_tcp.CRLF ||
'--MIME.Bound' || utl_tcp.CRLF ||
'Content-Type: text/plain; charset=iso-8859-1'|| utl_tcp.CRLF ||
'Content-Disposition: inline;' || utl_tcp.CRLF ||
'Content-Transfer_Encoding: 8bit'|| utl_tcp.CRLF;-- ||

--Se negocia la transaccion con el servidor SMTP
UTL_SMTP.HELO(V_Conexion, C_DOMINIO);
UTL_SMTP.MAIL(V_Conexion, C_REMITENTE);
UTL_SMTP.RCPT(V_Conexion, PI_DESTINATARIO);

--Si se desea enviar copia de este correo usar: UTL_SMTP.RCPT(V_Conexion, destinatariosEnCopia);

UTL_SMTP.OPEN_DATA(V_Conexion);

--Se escribe la cabecera
UTL_SMTP.WRITE_DATA(V_Conexion, msg);
--Para permitir caracteres "raros" (tildes, simbolos, ...)
-- UTL_SMTP.WRITE_DATA(V_Conexion,'MIME-version: 1.0' || V_Crlf);
-- UTL_SMTP.WRITE_DATA(V_Conexion,'Content-Type: text/plain; charset=iso-8859-1' || V_Crlf);

-- Se procesa el mensaje, separandolo previamente del V_Cabecera con un CRLF.
cadena := pi_cuerpo;
nPos := Instr (cadena ,chr(10) , 1);
WHILE (nPos<>0) LOOP
UTL_SMTP.WRITE_DATA(V_Conexion, V_Crlf || Substr(cadena, 1, nPos-1));
cadena := Rtrim(Ltrim(Substr(cadena ,nPos+1)));
nPos := Instr (cadena ,chr(10) , 1);
END LOOP;
UTL_SMTP.WRITE_DATA(V_Conexion, V_Crlf || cadena || V_Crlf);
UTL_SMTP.WRITE_DATA(V_Conexion,utl_tcp.CRLF);


-- Codigo anyadido para adjuntar correo
utl_smtp.write_data( V_Conexion, '--MIME.Bound' || utl_tcp.CRLF);
utl_smtp.write_data( V_Conexion, 'Content-Type: application/octet-stream; name="' || PI_TDOCADJ || '"' || utl_tcp.crlf);
utl_smtp.write_data( V_Conexion, 'Content-Disposition: attachment; filename="' || PI_TDOCADJ || '"' || utl_tcp.crlf);
utl_smtp.write_data( V_Conexion, 'Content-Transfer-Encoding: base64' || utl_tcp.crlf );
utl_smtp.write_data( V_Conexion, utl_tcp.crlf );

v_length := dbms_lob.getlength(PI_DOCADJ);
<<while_loop>>
while v_offset < v_length loop
dbms_lob.read( PI_DOCADJ, v_buffer_size, v_offset, v_raw );
utl_smtp.write_raw_data(V_Conexion, utl_encode.base64_encode(v_raw) );
utl_smtp.write_data( V_Conexion, utl_tcp.crlf );
v_offset := v_offset + v_buffer_size;
end loop while_loop;

utl_smtp.write_data( V_Conexion, utl_tcp.crlf );

utl_smtp.write_data( V_Conexion, '--MIME.Bound--'); -- End MIME mail
utl_smtp.write_data( V_Conexion, utl_tcp.crlf );

-- FIN Codigo anyadido para adjuntar correo

--Cerramos la conexion
UTL_SMTP.close_data(V_Conexion);
UTL_SMTP.quit(V_Conexion);

UPDATE GI_CORREO_ENVIADO SET T_ENVIADO = 'S', F_ENVIO=sysdate WHERE C_CORREO_ENVIADO = PI_C_CORREO_ENVIADO;
COMMIT;

EXCEPTION
WHEN UTL_SMTP.INVALID_OPERATION THEN
GIPK_LOG_PROCESOS.PR_InsertaError('PR_Enviar_Correo','Operacion no valida durante la transaccion SMTP.');
dbms_output.put_line(sqlerrm);
WHEN UTL_SMTP.TRANSIENT_ERROR THEN
GIPK_LOG_PROCESOS.PR_InsertaError('PR_Enviar_Correo','Problemas temporales al enviar el e-mail. Intentelo mas tarde.');
dbms_output.put_line(sqlerrm);
WHEN UTL_SMTP.PERMANENT_ERROR THEN
GIPK_LOG_PROCESOS.PR_InsertaError('PR_Enviar_Correo','Error en el codigo para la transaccion SMTP.');
dbms_output.put_line(sqlerrm);
END PR_Enviar_Correo_Con_Adjunto;

  Respuesta:  Alixandro Florian
A ver si esto te sirve.

Este ejempo fue probado y funciona.

---------------------------------------------------------------------
-- Procedimiento para Enviar Correos desde la Base de Datos Oracle --
-- Esto aplica para las versiones 8i (8.1.6) en adelante --
---------------------------------------------------------------------
DECLARE
Servidor VARCHAR2(64) := '192.188.173.12' ;
De VARCHAR2(64) := '[email protected]';
Asunto VARCHAR2(64) := 'Mensaje MultiLinea' ;
Para VARCHAR2(64) := '[email protected]' ;
CC VARCHAR2(64) := '[email protected]' ;
BCC1 VARCHAR2(64) := '[email protected]' ;
BCC2 VARCHAR2(64) := '[email protected]' ;
BCC VARCHAR2(64) := BCC1||','||BCC2 ;
ENTER VARCHAR2(10) := Chr(13)||Chr(10) ;
Conexion UTL_SMTP.connection;
--
BEGIN
Conexion := UTL_SMTP.Open_Connection(Servidor, 25);
UTL_SMTP.Helo(Conexion, Servidor);
UTL_SMTP.Mail(Conexion, De) ;
UTL_SMTP.Rcpt(Conexion, Para) ;
UTL_SMTP.Rcpt(Conexion, CC) ;
UTL_SMTP.Rcpt(Conexion, BCC1) ;
UTL_SMTP.Rcpt(Conexion, BCC2) ;
UTL_SMTP.Open_Data(Conexion) ;
UTL_SMTP.Write_Data(Conexion, 'Date: ' || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') || ENTER);
UTL_SMTP.Write_Data(Conexion, 'From: ' || De || ENTER);
UTL_SMTP.Write_Data(Conexion, 'Subject: ' || Asunto || ENTER);
UTL_SMTP.Write_Data(Conexion, 'To: ' || Para || ENTER);
UTL_SMTP.Write_Data(Conexion, 'CC: ' || CC || ENTER);
UTL_SMTP.Write_Data(Conexion, 'BCC: ' || BCC || ENTER);
UTL_SMTP.Write_Data(Conexion, 'DataBase Oracle 9i' || ENTER);
--
FOR Linea IN 1 .. 10 LOOP
UTL_SMTP.Write_Data(Conexion, 'Este es un Mensaje de Prueba. Linea No. ' || To_Char(Linea) || ENTER);
END LOOP;
--
UTL_SMTP.close_data(Conexion);
UTL_SMTP.quit(Conexion);
END;
/