A friend of mine took the Microsoft example proc and produced a more robust version. It defaults to using the servername from @@servername if one is not provided, and it can send file attachments:
master
PROCEDURE [dbo].[sp_sendmail_CDOSYS]
varchar(100) ,
varchar(100)=" ",
varchar(4000) =" ",
int = 1, -- 0=low, 1=normal, 2=high
varchar(100)= " " ,
varchar(100)= " " ,
varchar(8000)=NULL, /* seperated by ; */
int = 0,
varchar(100)= null ,
varchar(255) = null,
varchar(255) = null,
varchar(255) = null
AS
SELECT @Server = isnull(@Server, @@SERVERNAME)
Select @From = isnull(@From, @@SERVERNAME)
Declare @iMsg int
Declare @hr int
Declare @int int
Declare @Tmp varchar(8000)
Declare @source varchar(255)
Declare @description varchar(500)
Declare @output varchar(1000)
EXEC @hr = sp_OACreate 'CDO.Message', @iMsg OUT
EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value','2'
EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', @Server
if @UserName is not null and @password is not null
begin
EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/SendUserName").Value', @UserName
EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/SendPassword").Value', @Password
end
EXEC @hr = sp_OASetProperty @iMsg, 'To', @To
EXEC @hr = sp_OASetProperty @iMsg, 'From', @From
EXEC @hr = sp_OASetProperty @iMsg, 'Subject', @Subject
EXEC @hr = sp_OASetProperty @iMsg, 'CC', @cc
EXEC @hr = sp_OASetProperty @iMsg, 'BCC', @BCC
len(@Attachments)>0
begin
while len(@Attachments)>0
begin
set @int = charindex(';',@Attachments)
if @int > 0
begin
set @Tmp = left(@Attachments,@int-1)
set @Attachments = right(@Attachments,(len(@Attachments)-@int))
end else
begin
set @Tmp = @Attachments
set @Attachments = ''
end /* Add the Attachment */
Print @Tmp
EXEC @hr= sp_OAMethod @iMsg, 'AddAttachment', NULL, @Tmp
end
end
@HTMLFormat <> 1
EXEC @hr = sp_OASetProperty @iMsg, 'TextBody', @Body
EXEC @hr = sp_OASetProperty @iMsg, 'HTMLBody', @Body
EXEC @hr = sp_OAMethod @iMsg, 'Configuration.Fields.Update', null
EXEC @hr = sp_OAMethod @iMsg, 'Send', NULL
IF @hr <>0
select @hr
BEGIN
EXEC @hr = sp_OAGetErrorInfo NULL, @source OUT, @description OUT
IF @hr = 0
BEGIN
SELECT @output = ' Source: ' + @source
PRINT @output
SELECT @output = ' Description: ' + @description
PRINT @output
END
ELSE
BEGIN
PRINT ' sp_OAGetErrorInfo failed.'
RETURN
END
END
EXEC @hr = sp_OADestroy @iMsg
Kindest Regards,
Clayton