Acá va el detalle de como enviar correo electrónico desde SQL Server, utilizando Stored Procedures y utilizando como servidor de salida una cuenta de gmail.
1) En el servidor Sql, debemos habilitar la opción “Ole Automation Procedures” que por defecto está deshabilitada, para que nos permita llamar aplicaciones externas al servidor. Desde la consola ejecutamos la siguiente consulta:
EXEC sp_configure 'show advanced option', 1
RECONFIGURE
EXEC sp_configure 'Ole Automation Procedures', 1;
RECONFIGURE
2) Instalamos la aplicación gratuita easendmail de emailarchitect.net que permite realizar el dialogo del servidor smtp con gmail, entre otros proveedores de correo.
3) Creamos un sp que nos permitirá llamar a esta aplicacion y enviar correo. Gracias emailarchitect.net
CREATE PROCEDURE [dbo].[usp_SendTextEmail] @ServerAddr nvarchar(128),
@From nvarchar(128),
@To nvarchar(1024),
@Subject nvarchar(256),
@Bodytext nvarchar(max) = 'This is a test text email from MS SQL server, do not reply.',
@User nvarchar(128) = '',
@Password nvarchar(128) = '',
@SSLConnection int = 0,
@ServerPort int = 25
AS
DECLARE @hr int
DECLARE @oSmtp int
DECLARE @result int
DECLARE @description nvarchar(255)
EXEC @hr = sp_OACreate 'EASendMailObj.Mail',@oSmtp OUT
If @hr <> 0
BEGIN
PRINT 'Please make sure you have EASendMail Component installed!'
EXEC @hr = sp_OAGetErrorInfo @oSmtp, NULL, @description OUT
IF @hr = 0
BEGIN
PRINT @description
END
RETURN
End
EXEC @hr = sp_OASetProperty @oSmtp, 'LicenseCode', 'TryIt'
EXEC @hr = sp_OASetProperty @oSmtp, 'ServerAddr', @ServerAddr
EXEC @hr = sp_OASetProperty @oSmtp, 'ServerPort', @ServerPort
EXEC @hr = sp_OASetProperty @oSmtp, 'UserName', @User
EXEC @hr = sp_OASetProperty @oSmtp, 'Password', @Password
EXEC @hr = sp_OASetProperty @oSmtp, 'FromAddr', @From
EXEC @hr = sp_OAMethod @oSmtp, 'AddRecipientEx', NULL, @To, 0
EXEC @hr = sp_OASetProperty @oSmtp, 'Subject', @Subject
EXEC @hr = sp_OASetProperty @oSmtp, 'BodyText', @BodyText
If @SSLConnection > 0
BEGIN
EXEC @hr = sp_OAMethod @oSmtp, 'SSL_init', NULL
END
PRINT 'Start to send email ...'
EXEC @hr = sp_OAMethod @oSmtp, 'SendMail', @result OUT
If @hr <> 0
BEGIN
EXEC @hr = sp_OAGetErrorInfo @oSmtp, NULL, @description OUT
IF @hr = 0
BEGIN
PRINT @description
END
RETURN
End
If @result <> 0
BEGIN
EXEC @hr = sp_OAMethod @oSmtp, 'GetLastErrDescription', @description OUT
PRINT 'failed to send email with the following error:'
PRINT @description
END
ELSE
BEGIN
PRINT 'Email was sent successfully!'
END
EXEC @hr = sp_OADestroy @oSmtp
Go
4) Ya estamos en condiciones de enviar email desde Sql Server. En una ventana nueva de la consola podemos ejecutar:
/* Gmail SMTP server address */
DECLARE @ServerAddr nvarchar(128)
Set @ServerAddr = 'smtp.gmail.com'
/* Set your Gmail email address */
DECLARE @From nvarchar(128)
Set @From = 'direccionorigen@gmail.com'
DECLARE @To nvarchar(1024)
/*You can input multiple recipients and use comma (,) to separate multiple addresses */
Set @To = 'direcciondestino@gmail.com'
DECLARE @Subject nvarchar(256)
Set @Subject = 'prueba envio de correo desde sql server'
DECLARE @Bodytext nvarchar(512)
Set @BodyText = 'Este es un mesaje de prueba. No responda.'
/* Gmail user authentication should use your Gmail email address as the user name. */
DECLARE @User nvarchar(128)
Set @User = 'direccionorigen@gmail.com'
DECLARE @Password nvarchar(128)
Set @Password = 'su password'
/* Enable SSL/TLS */
DECLARE @SSL int
Set @SSL = 1
/* If you want to use TLS, please set it to 25 or 587 */
DECLARE @Port int
Set @Port = 465
PRINT 'start to send email ...'
exec usp_SendTextEmail @ServerAddr, @From, @To, @Subject, @BodyText, @User, @Password, @SSL, @Port
Ejecutamos y listo.