Search

Send Mail through SQL (using CDONTS)

SQL Code Snippets to send emailBelow are snippets which can be used to send emails from SQL Server

--Start SP # 2 Send Mail through SQL (using CDONTS)
CREATE PROCEDURE [dbo].[usp_send_cdontsmail]
@From varchar(100),
@To
  varchar(100),
@Subject varchar(100),
@Body varchar(4000),
@CC
  varchar(100) = null,
@BCC varchar(100) = null
AS
Declare @MailID
  int
Declare @hr int
EXEC @hr = sp_OACreate 'CDONTS.NewMail', @MailID OUT
EXEC @hr = sp_OASetProperty @MailID, 'From',@From
EXEC @hr = sp_OASetProperty @MailID, 'Body', @Body
EXEC @hr = sp_OASetProperty @MailID, 'BCC',@BCC
EXEC @hr = sp_OASetProperty @MailID, 'CC', @CC
EXEC @hr = sp_OASetProperty @MailID, 'Subject', @Subject
EXEC @hr = sp_OASetProperty @MailID, 'To', @To
EXEC @hr = sp_OAMethod @MailID, 'Send', NULL
EXEC @hr = sp_OADestroy @MailID

exec usp_send_cdontsmail
  ''dummyuserreceipient@gmail.com',''dummyuserreceipient@gmail.com','Test of CDONTS','It works'

--End SP # 2 Send Mail through SQL