Simple email with CDONTS

,

Here is a strait forward script that allows you to use CDONTS to send email.  Note that the Subject, From, CC and BCC are all optional parameters.  Because it uses a fully qualified path, this procedure can live in any database on the server.  CDONTS will need to be installed on the server.

CREATE Proc usp_Send_Mail
	@To varchar(100),
	@Body varchar(4000),
	@Subject varchar(100) = ' ',
	@From varchar(100) = ' ',
	@CC varchar(100) = null,
	@BCC varchar(100) = null
AS
	DECLARE @MailID int
	DECLARE @Mail int

EXEC @Mail = master..sp_OACreate 'CDONTS.NewMail', @MailID OUT
EXEC @Mail = master..sp_OASetProperty @MailID, 'From', @From
EXEC @Mail = master..sp_OASetProperty @MailID, 'Body', @Body
EXEC @Mail = master..sp_OASetProperty @MailID, 'BCC', @BCC
EXEC @Mail = master..sp_OASetProperty @MailID, 'CC', @CC
EXEC @Mail = master..sp_OASetProperty @MailID, 'Subject', @Subject
EXEC @Mail = master..sp_OASetProperty @MailID, 'To', @To
EXEC @Mail = master..sp_OAMethod @MailID, 'Send', Null
EXEC @Mail = master..sp_OADestroy @MailID

GO

Rate

Share

Share

Rate