CREATE PROC CdoSendMail
@From varchar(255),
@To varchar(255),
@Subject varchar(255),
@BodyFormat tinyint,
@Importance tinyint,
@Body varchar(8000)
AS
DECLARE @ObjectHandle int
DECLARE @HRESULT int
DECLARE @ErrorSource varchar(255)
DECLARE @ErrorDescrip varchar(255)
--Create the NewMail object
EXEC @HRESULT = sp_OACreate 'CDONTS.NewMail',
@ObjectHandle OUT
IF @HRESULT <> 0 GOTO ErrorHandler
--Set key properties for the NewMail object
EXEC @HRESULT = sp_OASetProperty @ObjectHandle, 'From',
@From
IF @HRESULT <> 0 GOTO ErrorHandler
EXEC @HRESULT = sp_OASetProperty @ObjectHandle, 'To',
@To
IF @HRESULT <> 0 GOTO ErrorHandler
EXEC @HRESULT = sp_OASetProperty @ObjectHandle,
'Subject',
@Subject
IF @HRESULT <> 0 GOTO ErrorHandler
EXEC @HRESULT = sp_OASetProperty @ObjectHandle,
'MailFormat', 0
IF @HRESULT <> 0 GOTO ErrorHandler
EXEC @HRESULT = sp_OASetProperty @ObjectHandle, 'Body',
@Body
IF @HRESULT <> 0 GOTO ErrorHandler
/*
--Add a couple of file attachments, just because we can
EXEC @HRESULT = sp_OAMethod @ObjectHandle,
'AttachFile(Source := “c:\query.txt”,
FileName := “Sample SQL Query”)'
IF @HRESULT <> 0 GOTO ErrorHandler
EXEC @HRESULT = sp_OAMethod @ObjectHandle,
'AttachFile(Source := “c:\test.udl”,
FileName := “Sample Data Link”)'
IF @HRESULT <> 0 GOTO ErrorHandler
*/
--Now send the mail message
EXEC @HRESULT = sp_OAMethod @ObjectHandle, 'Send'
IF @HRESULT <> 0 GOTO ErrorHandler
RETURN 0 --No error
ErrorHandler:
EXEC sp_OAGetErrorInfo @ObjectHandle, @ErrorSource OUT,
@ErrorDescrip OUT
SELECT HRESULT = Convert(varbinary(4),@HRESULT),
Source =
@ErrorSource, Description = @ErrorDescrip
RETURN @HRESULT --Pass back error
GO