Technical Article

Send mail from  SQL Server using Jmail

,

For greater information of this tool can obtain it in
http://www.dimac.net/
Mail in format HTML or TEXT can be sent can be sent attached archives.
It is a tool to send and to receive electronic mail from any PC or SERVER without the use of a program of mail or a mail server as Eudora, Exchange or Outlook

IF EXISTS (SELECT * FROM dbo.SYSOBJECTS 
           WHERE ID = OBJECT_ID(N'[dbo].[envia_mail]') 
           AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[envia_mail]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROC envia_mail
/*=============================================================================================
   Fecha......28/Nov/2003   Marco A. Serrano
   Proposito..Enviar desde SQL Server Correo electronico usando Jmail
   Input...@Vchsender     WHO SENDS THE MESSAGE, (JPALACIOS@HOTMAIL.COM)
@Vchsendername    NAME OF THAT SENDS THE MESSAGE(OPTIONAL), (JOSE LUIS PALACIOS)
@Vchrecipient     To WHO IS SENT The MESSAGE, (BVELASCO@YAHOO.COM;JUMENDEZ@HOTMAIL.COM)
@VchrecipientBCC  To WHO IS SENT HIDDEN COPY To HIM Of the MESSAGE(OPTIONAL),
@VchrecipientCC   To WHO IS SENT COPY To HIM Of the MESSAGE(OPTIONAL),
@Vchattachment    ATTACHED FILE (OPTIONAL), (C:\MY DOCUMENTS\MY.DOC)
@Vchsubject       SUBJECT OF THE MESSAGE, MAXIMUM 255 CHARACTERS,
@Vchmailbody      BODY OF THE MESSAGE, MAXIMUM 255 CHARACTERS,
@VchBodyPart      IN ORDER TO ADD TEXT TO THE BODY OF THE MESSAGE,
@VchContentType   CONTAINED TYPE(HTML ó TEXTO) OPTIONAL, IF IT IS NOT INCLUDED BY DEFAULT IT IS TEXT
   output....Mail 
   NOTE: For greater information of this utileria can obtain it in http://www.dimac.net/
 ==============================================================================================
    Date    |  modified    |   Intention
 ==============================================================================================*/
@Vchsender VARCHAR(100),
@Vchsendername VARCHAR(100)='',
@Vchrecipient VARCHAR(1000),
@VchrecipientBCC VARCHAR(1000)='',
@VchrecipientCC VARCHAR(1000)='',
@Vchattachment VARCHAR(100)='',
@Vchsubject VARCHAR(255),
@Vchmailbody VARCHAR(255),
@VchBodyPart VARCHAR(8000),
@VchContentType VARCHAR(50)=''
AS
--VARIABLES PARA DESGLOSAR LA LISTA DE DESTINATARIOS Y/O PARA DIVIDIR LA VARIABLE @VchBodyPart E INSERTAR TEXTO
--AL CUERPO DEL MENSAJE CUENDO ESTE SOBREPASE LOS 255 CARACTERES
DECLARE @Vchstring VARCHAR(8000),   @Vchstring_length INT, @VchstrToEmail VARCHAR(255),
@Vchsubstring NVARCHAR(50), @Intstartpos INT,      @Intendpos INT
--VARIABLES PARA EL OBJECTO JMAIL
DECLARE@Intobject INT, @hr INT, @Intrc INT, @Vchoutput VARCHAR(400),
@Vchdescription VARCHAR(400),  @Vchsource VARCHAR(400),
@Vchdominio VARCHAR(100),--SERVIDOR DE DOMINIO (CCPUEBLA.COM.MX)
@Vchserveraddress VARCHAR(255)--DIRECCION O NOMBRE DEL SERVIDOR PROXI
SET NOCOUNT ON
BEGIN TRAN
--OBTENEMOS EL DOMINIO Y DIRECCION DEL SERVIDOR DE CORREO
SELECT @Vchdominio = RTRIM(servidor_dominio), @Vchserveraddress = RTRIM(servidor_direccion)
FROM configuracion_servidor_proxi

--CREA UNA INSTANCIA DEL OBJETO "jmail.smtpmail" EN UNA INSTANCIA DE MICROSOFT SQL SERVER
--Y ESTABLECE LOS VALORES DE LAS PROPIEDADES DEL OBJETO "jmail.smtpmail"
EXEC @hr = SP_OACreate 'jmail.smtpmail', @Intobject OUT --Crea el objeto
EXEC @hr = sp_OASetProperty @Intobject, 'ISOEncodeHeaders', 'false' --Decodifica caracteres segun el standard iso-8859-1
EXEC @hr = sp_OASetProperty @Intobject, 'charset', 'iso-8859-1'  --Setea a caracteres iso-8859-1
EXEC @hr = sp_OASetProperty @Intobject, 'Maildomain', @Vchdominio
EXEC @hr = sp_OASetProperty @Intobject, 'Logging', true
EXEC @hr = SP_OASetProperty @Intobject, 'Sender', @Vchsender
EXEC @hr = SP_OASetProperty @Intobject, 'ServerAddress', @Vchserveraddress
--SI SE REQUIERE FORMATO HTML
IF @VchContentType <> ''
EXEC @hr = SP_OASetProperty @Intobject, 'ContentType', @VchContentType --Para convertir el cuerpo del mensaje a formato HTML

-- ADICIONA LA LISTA DE CORREO ELECTRONICO AL METODO "ADDRECIPIENT" ESTE WHILE ES PARA PREVENIR
-- ENVIAR A VARIOS DESTINATARIOS
SELECT @Vchsubstring = ''
SELECT @Vchstring = @Vchrecipient
SELECT @Vchstring_length = len(@Vchstring)
SELECT @Intstartpos = 1
SELECT @Intendpos = 1
WHILE @Intstartpos <= @Vchstring_length and @Intendpos <= @Vchstring_length + 1
BEGIN
IF SUBSTRING(@Vchstring,@Intendpos,1) = ';' or @Intendpos > @Vchstring_length
BEGIN
SELECT @VchstrToEmail = SUBSTRING(@Vchstring,@Intstartpos,@Intendpos-@Intstartpos)
EXEC @hr = SP_OAMethod @Intobject, 'AddRecipient', NULL , @VchstrToEmail
SELECT @Intstartpos = @Intendpos + 1
SELECT @Intendpos = @Intstartpos + 1
END
SELECT @Intendpos = @Intendpos + 1
END
--INSERTAMOS EL ASUNTO Y CUERPO DEL MENSAJE
EXEC @hr = SP_OASetProperty @Intobject, 'Subject', @Vchsubject
--INSERTAMOS EL CUERPO DEL MENSAJE
EXEC @hr = SP_OASetProperty @Intobject, 'Body', @Vchmailbody
--SI ES MAS GRANDE EL CUERPO DEL MENSAJE ADICIONAMOS EL TEXTO EXTRA
SELECT @Vchsubstring = ''
SELECT @Vchstring = @VchBodyPart
SELECT @Vchstring_length = 8000--len(@Vchstring)
SELECT @Intstartpos = 1
SELECT @Intendpos = 80
WHILE @Intendpos <= @Vchstring_length
BEGIN
SELECT @VchBodyPart = SUBSTRING(@Vchstring,@Intstartpos,80)
IF Len(@VchBodyPart) = 0
BEGIN
BREAK;
END
EXEC @hr = sp_OAMethod @Intobject, 'AppendText', null, @VchBodyPart
SELECT @Intstartpos = @Intendpos + 1
SELECT @Intendpos = @Intendpos + 80
END
--SI EL MENSAJE VA EN FORMATO HTML CIERRA EL CUERPO DEL MENSAJE
IF @VchContentType = 'text/html'
BEGIN
SELECT @VchBodyPart = '</FONT></BODY></HTML>'
EXEC @hr = sp_OAMethod @Intobject, 'AppendText', null, @VchBodyPart
END
--SI SE NECESITARA ENVIAR UN ARCHIVO ADJUNTO
IF NOT(@Vchattachment='')
EXEC @hr = sp_OAMethod @Intobject, 'Addattachment', NULL , @Vchattachment
--SI SE REQUIERE ENVIAR UNA COPIA DEL MENSAJE OCULTA
IF NOT(@VchrecipientBCC='')
EXEC @hr = sp_OAMethod @Intobject, 'AddRecipientBCC', NULL , @VchrecipientBCC
--SI SE REQUIERE ENVIAR ADEMAS COPIAS DEL MENSAJE
IF NOT(@VchrecipientCC='')
EXEC @hr = sp_OAMethod @Intobject, 'AddRecipientCC', NULL , @VchrecipientCC
--PARA ENVIAR EL NOMBRE DE LA PERSONA QUE ENVIA EL MENSAJE
IF NOT(@Vchsendername='')
EXEC @hr = sp_OASetProperty @Intobject, 'SenderName', @Vchsendername
--EJECUTAMOS EL OBJETO CREADO PARA ENVIAR EL CORREO
EXEC @hr = sp_OAMethod @Intobject, 'execute', NULL

--CACHAMOS POSIBLES ERRORES
EXEC @hr = sp_OAGetErrorInfo @Intobject, @Vchsource OUT, @Vchdescription OUT
IF @hr <> 0
BEGIN
IF @@TRANCOUNT > 0 ROLLBACK TRAN
EXEC @hr = SP_OADestroy @Intobject
RAISERROR (@Vchdescription , 16, 1)
RETURN
END
--DESTRUIMOS EL OBJETO CREADO
EXEC @hr = SP_OADestroy @Intobject
COMMIT TRAN
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

GRANT  EXECUTE  ON [dbo].[envia_mail]  TO [public]
GO

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating