Send Mail T-SQL

Posted by Isaac Blum at 2 November 2009

Category: Uncategorized

1. Create a file named sendmail.vbs and save on c:\winnt.
2. Put this code:
Set objEmail = CreateObject(“CDO.Message”)
objEmail.From = “yourmail@yourserver.com”

objEmail.to = WScript.Arguments(1)
objEmail.Subject = WScript.Arguments(2)
objEmail.Textbody = WScript.Arguments(3)

objEmail.Configuration.Fields.Item (“http://schemas.microsoft.com/cdo/configuration/sendusing”) = 2
objEmail.Configuration.Fields.Item (“http://schemas.microsoft.com/cdo/configuration/smtpserver”) = “your_SMTP_server_IP_or_name”
objEmail.Configuration.Fields.Item (“http://schemas.microsoft.com/cdo/configuration/smtpserverport”) = 25

objEmail.Configuration.Fields.Update
objEmail.Send

set objEmail = Nothing

3. And now, complete creating a stored procedure:
USE MASTER
GO
CREATE PROCEDURE sp_sendmail @mail varchar(150), @subject varchar(150), @textbody varchar(800) AS
DECLARE @MSG_SENT VARCHAR(1200)
SELECT @MSG_SENT = ‘cscript c:\winnt\sendmail.vbs /p “‘ + @mail + ‘” “‘ + @subject + ‘” “‘ + @textbody + ‘”‘

exec master.dbo.xp_cmdshell @MSG_SENT, NO_OUTPUT
SELECT ‘Mail sent!’
GO
******************************************************************

Ok, allright!

Send a mail to test:

EXEC master.dbo.sp_sendmail ‘yourto@yourserver.com’, ‘subject’, ‘message’

  • Share/Bookmark

Leave a Reply

Leave a Reply
  • (required)
  • (required) (will not be published)

  • Archives

  • Pages

  • Tags

  • More

Get Adobe Flash playerPlugin by wpburn.com wordpress themes