This article will show you how to use CDOSYS library (Provided with most of windows versions including win 2000, xp, 2003) to send mails from sql server.
I have created a COM component so you can send emails with long body text. |
Click here to copy the following block | use master
go
Create Procedure dbo.sp_SQLSMTPMail @To varchar(2048) = NULL, @Body text = NULL, @Subject varchar(255) = NULL, @From varchar(128) = NULL, @CC varchar(2048) = NULL, @BCC varchar(2048) = NULL, @Attachments varchar(8000)=NULL, @IsHTMLFormat Bit = 0,
@SMTPServer varchar(255) = '' As
DECLARE @object int DECLARE @hr int Declare @ErrMssg varchar(255) Declare @Attachment varchar(1024) Declare @iPos int Declare @iRtn int Declare @ErrSource varchar(255) Declare @ErrDescription varchar(255)
Select @To = Replace(@To, ';', ',') Select @CC = Replace(@CC, ';', ',') Select @BCC = Replace(@BCC, ';', ',')
If @To is null Begin RAISERROR('You must supply at least 1 recipient.',16,1) Return End
If @From is null Set @From = 'SQL-' + Replace(@@servername,'\','_')
EXEC @hr = sp_OACreate 'SMTPMail.Mail', @object OUT IF @hr <> 0 BEGIN EXEC sp_OAGetErrorInfo @object, @ErrSource Out, @ErrDescription Out Print @ErrSource Print @ErrDescription Return END
EXEC @hr = sp_OASetProperty @object, 'MailFrom',@From IF @hr <> 0 BEGIN EXEC sp_OAGetErrorInfo @object, @ErrSource Out, @ErrDescription Out Print @ErrSource Print @ErrDescription Return END
EXEC @hr = sp_OASetProperty @object, 'MailBody',@Body IF @hr <> 0 BEGIN EXEC sp_OAGetErrorInfo @object, @ErrSource Out, @ErrDescription Out Print @ErrSource Print @ErrDescription Return END
EXEC @hr = sp_OASetProperty @object, 'MailSubject',@Subject IF @hr <> 0 BEGIN EXEC sp_OAGetErrorInfo @object, @ErrSource Out, @ErrDescription Out Print @ErrSource Print @ErrDescription Return END
EXEC @hr = sp_OASetProperty @object, 'MailTo', @To IF @hr <> 0 BEGIN EXEC sp_OAGetErrorInfo @object, @ErrSource Out, @ErrDescription Out Print @ErrSource Print @ErrDescription Return END
EXEC @hr = sp_OASetProperty @object, 'MailCC', @CC IF @hr <> 0 BEGIN EXEC sp_OAGetErrorInfo @object, @ErrSource Out, @ErrDescription Out Print @ErrSource Print @ErrDescription Return END
EXEC @hr = sp_OASetProperty @object, 'MailBCC', @BCC IF @hr <> 0 BEGIN EXEC sp_OAGetErrorInfo @object, @ErrSource Out, @ErrDescription Out Print @ErrSource Print @ErrDescription Return END
EXEC @hr = sp_OASetProperty @object, 'IsBodyHTML', @IsHTMLFormat IF @hr <> 0 BEGIN EXEC sp_OAGetErrorInfo @object, @ErrSource Out, @ErrDescription Out Print @ErrSource Print @ErrDescription Return END
EXEC @hr = sp_OASetProperty @object, 'SMTPServer', @SMTPServer IF @hr <> 0 BEGIN EXEC sp_OAGetErrorInfo @object, @ErrSource Out, @ErrDescription Out Print @ErrSource Print @ErrDescription Return END
EXEC @hr = sp_OASetProperty @object, 'AttachFiles', @Attachments IF @hr <> 0 BEGIN EXEC sp_OAGetErrorInfo @object, @ErrSource Out, @ErrDescription Out Print @ErrSource Print @ErrDescription Return END
EXEC @hr = sp_OAMethod @object, 'Send', NULL IF @hr <> 0 BEGIN EXEC sp_OAGetErrorInfo @object, @ErrSource Out, @ErrDescription Out Print @ErrSource Print @ErrDescription END Else Print 'Mail sent to ' + @To
Cleanup: EXEC @hr = sp_OADestroy @object IF @hr <> 0 BEGIN EXEC sp_OAGetErrorInfo @object, @ErrSource Out, @ErrDescription Out Print @ErrSource Print @ErrDescription Return END
Return
GO |
|