ALTER Procedure dbo.sp_SQLSMTPMail @To varchar(2048) = null, @Body varchar(8000) = '', @Subject varchar(255) = null, @Attachments varchar(1024) = null, @Query varchar(8000) = null, @From varchar(128) = null, @CC varchar(2048) = '', @BCC varchar(2048) = '', @IsHTMLFormat Bit = 0, @SMTPServer varchar(255) = 'localhost', @cSendUsing char(1) = '2', @Port varchar(3) = '25', @cAuthenticate char(1) = '0', @DSNOptions varchar(2) = '0', @Timeout varchar(2) = '30', @SenderName varchar(128) = null, @ServerName sysname = null As
Set nocount on
If @To = '?' Begin Print 'Syntax for sp_SQLSMTPMail (based on CDOSYS):' Print 'Exec master.dbo.sp_SQLSMTPMail' Print ' @To (varchar(2048)) - Recipient e-mail address list separating each with a '';'' ' Print ' or a '',''. Use a ''?'' to return the syntax.' Print ' @Body (varchar(8000)) - Text body; use embedded char(13) + char(10)' Print ' for carriage returns. The default is nothing' Print ' @Subject (varchar(255))) - E-mail subject. The default is a message from' Print ' @@servername.' Print ' @Attachments (varchar(1024)) - Attachment list separating each with a '';''.' Print ' The default is no attachments.' Print ' @Query (varchar(8000)) - In-line query or a query file path; do not ' Print ' use double quotes within the query.' Print ' @From (varchar(128)) - Sender list defaulted to @@ServerName.' Print ' @CC (varchar(2048)) - CC list separating each with a '';'' or a '',''' Print ' The default is no CC addresses.' Print ' @BCC (varchar(2048)) - Blind CC list separating each with a '';'' or a '',''' Print ' The default is no BCC addresses.' Print ' @IsHTMLFormat (Bit) - If 1 then Format of Mail will be HTML Mail otherwise Plain text' Print ' @SMTPServer (varchar(255)) - Network smtp server defaulted to your companies network' Print ' smtp server. Set this in the stored proc code.' Print ' @cSendUsing (char(1)) - Specifies the smpt server method, local or network. The' Print ' default is network, a value of ''2''.' Print ' @Port (varchar(3)) - The smtp server communication port defaulted to ''25''.' Print ' @cAuthenticate (char(1)) - The smtp server authentication method defaulted to ' Print ' anonymous, a value of ''0''.' Print ' @DSNOptions (varchar(2)) - The smtp server delivery status defaulted to none,' Print ' a value of ''0''.' Print ' @Timeout (varchar(2)) - The smtp server connection timeout defaulted to 30 seconds.' Print ' @SenderName (varchar(128)) - Primary sender name defaulted to @@ServerName.' Print ' @ServerName (sysname) - SQL Server to which the query is directed defaulted' Print ' to @@ServerName.' Print '' Print '' Print 'Example:' Print 'sp_SQLSMTPMail ''<user@mycompany.com>'', ''This is a test'', @SMTPServer = <network smtp relay server>' Print '' Print 'The above example will send an smpt e-mail to <user@mycompany.com> from @@ServerName' Print 'with a subject of ''Message from SQL Server <@@ServerName>'' and a' Print 'text body of ''This is a test'' using the network smtp server specified.' Print 'See the MSDN online library, Messaging and Collaboration, at ' Print 'http://www.msdn.microsoft.com/library/ for details about CDOSYS.' Print 'subheadings: Messaging and Collaboration>Collaboration Data Objects>CDO for Windows 2000>' Print 'Reference>Fields>http://schemas.microsoft.com/cdo/configuration/>smtpserver field' Print '' Print 'Be sure to set the default for @SMTPServer before compiling this stored procedure.' Print '' Return End
Declare @iMessageObjId int Declare @iHr int Declare @iRtn int Declare @iFileExists tinyint Declare @Cmd varchar(255) Declare @QueryOutPath varchar(50) Declare @dtDatetime datetime Declare @ErrMssg varchar(255) Declare @Attachment varchar(1024) Declare @iPos int Declare @ErrSource varchar(255) Declare @ErrDescription varchar(255)
Set @dtDatetime = getdate() Set @iHr = 0
If @To is null Begin Set @ErrMssg = 'You must supply at least 1 recipient.' Goto ErrMssg End
Select @To = Replace(@To, ';', ',') Select @CC = Replace(@CC, ';', ',') Select @BCC = Replace(@BCC, ';', ',')
If @ServerName is null Set @ServerName = @@servername
If @Subject is null Set @Subject = 'Message from SQL Server ' + @ServerName
If @From is null Set @From = 'SQL-' + Replace(@ServerName,'\','_')
If @SenderName is null Set @SenderName = 'SQL-' + Replace(@ServerName,'\','_')
EXEC @iHr = sp_OACreate 'CDO.Message', @iMessageObjId OUT IF @iHr <> 0 Begin Set @ErrMssg = 'Error creating object CDO.Message.' Goto ErrMssg End
EXEC @iHr = sp_OASetProperty @iMessageObjId, 'To', @To IF @iHr <> 0 Begin Set @ErrMssg = 'Error setting Message parameter "To".' Goto ErrMssg End
EXEC @iHr = sp_OASetProperty @iMessageObjId, 'Subject', @Subject IF @iHr <> 0 Begin Set @ErrMssg = 'Error setting Message parameter "Subject".' Goto ErrMssg End
EXEC @iHr = sp_OASetProperty @iMessageObjId, 'From', @From IF @iHr <> 0 Begin Set @ErrMssg = 'Error setting Message parameter "From".' Goto ErrMssg End
EXEC @iHr = sp_OASetProperty @iMessageObjId, 'CC', @CC IF @iHr <> 0 Begin Set @ErrMssg = 'Error setting Message parameter "CC".' Goto ErrMssg End
EXEC @iHr = sp_OASetProperty @iMessageObjId, 'BCC', @BCC IF @iHr <> 0 Begin Set @ErrMssg = 'Error setting Message parameter "BCC".' Goto ErrMssg End
EXEC @iHr = sp_OASetProperty @iMessageObjId, 'DSNOptions', @DSNOptions IF @iHr <> 0 Begin Set @ErrMssg = 'Error setting Message parameter "DSNOptions".' Goto ErrMssg End
EXEC @iHr = sp_OASetProperty @iMessageObjId, 'Sender', @SenderName IF @iHr <> 0 Begin Set @ErrMssg = 'Error setting Message parameter "Sender".' Goto ErrMssg End
If @Query is not null and @Query <> '' Begin If (Select object_id('tempdb.dbo.#fixeddrives')) > 0 Exec ('Drop table #fixeddrives') Create table #fixeddrives( Drive char(1) null, FreeSpace varchar(15) null)
Insert into #fixeddrives Exec master.dbo.xp_fixeddrives
Select @QueryOutPath = Drive + ':\TempQueryOut' + ltrim(str(datepart(hh,getdate()))) + ltrim(str(datepart(mi,getdate()))) + ltrim(str(datepart(ss,getdate()))) + ltrim(str(datepart(ms,getdate()))) + '.txt' from #fixeddrives where FreeSpace = (select max(FreeSpace) from #fixeddrives ) If Left(@Query, 35) like '\\%\%' or Left(@Query, 5) like '_:\%' Begin Select @Cmd = 'osql /S' + @ServerName + ' /E /i' + convert(varchar(1024),@Query) + ' /o' + @QueryOutPath + ' -n -w5000 ' End Else Begin Select @Cmd = 'osql /S' + @ServerName + ' /E /Q"' + @Query + '" /o' + @QueryOutPath + ' -n -w5000 ' End
Exec master.dbo.xp_cmdshell @Cmd, no_output
If (Select object_id('tempdb.dbo.#fileexists')) > 0 Exec ('Drop table #fileexists') Create table #fileexists( FileExists tinyint null, FileIsDirectory tinyint null, ParentDirectoryExists tinyint null)
Insert into #fileexists exec master.dbo.xp_fileexist @QueryOutPath
If (select FileExists from #fileexists) = 1 Begin Select @iFileExists = 1
If @Attachments is null Select @Attachments = @QueryOutPath Else Select @Attachments = @Attachments + '; ' + @QueryOutPath End End
If @Attachments is not null Begin If right(@Attachments,1) <> ';' Select @Attachments = @Attachments + '; ' Select @iPos = CharIndex(';', @Attachments, 1) While @iPos > 0 Begin Select @Attachment = ltrim(rtrim(substring(@Attachments, 1, @iPos -1))) Select @Attachments = substring(@Attachments, @iPos + 1, Len(@Attachments)-@iPos) EXEC @iHr = sp_OAMethod @iMessageObjId, 'AddAttachment', @iRtn Out, @Attachment IF @iHr <> 0 Begin EXEC sp_OAGetErrorInfo @iMessageObjId, @ErrSource Out, @ErrDescription Out Select @Body = @Body + char(13) + char(10) + char(13) + char(10) + char(13) + char(10) + 'Error adding attachment: ' + char(13) + char(10) + @ErrSource + char(13) + char(10) + @Attachment End Select @iPos = CharIndex(';', @Attachments, 1) End End
if @IsHTMLFormat=1 begin EXEC @iHr = sp_OASetProperty @iMessageObjId, 'HTMLBody', @Body IF @iHr <> 0 Begin Set @ErrMssg = 'Error setting Message parameter "BodyFormat".' Goto ErrMssg End end else begin EXEC @iHr = sp_OASetProperty @iMessageObjId, 'TextBody', @Body IF @iHr <> 0 Begin Set @ErrMssg = 'Error setting Message parameter "TextBody".' Goto ErrMssg End end
EXEC @iHr = sp_OASetProperty @iMessageObjId, 'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', @SMTPServer IF @iHr <> 0 Begin Set @ErrMssg = 'Error setting Message configuraton field "smtpserver".' Goto ErrMssg End
EXEC @iHr = sp_OASetProperty @iMessageObjId, 'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value', @cSendUsing IF @iHr <> 0 Begin Set @ErrMssg = 'Error setting Message configuraton field "sendusing".' Goto ErrMssg End
EXEC @iHr = sp_OASetProperty @iMessageObjId, 'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/SMTPConnectionTimeout").Value', @Timeout IF @iHr <> 0 Begin Set @ErrMssg = 'Error setting Message configuraton field "SMTPConnectionTimeout".' Goto ErrMssg End
EXEC @iHr = sp_OASetProperty @iMessageObjId, 'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/SMTPServerPort").Value', @Port IF @iHr <> 0 Begin Set @ErrMssg = 'Error setting Message configuraton field "SMTPServerPort".' Goto ErrMssg End
EXEC @iHr = sp_OASetProperty @iMessageObjId, 'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/SMTPAuthenticate").Value', @cAuthenticate IF @iHr <> 0 Begin Set @ErrMssg = 'Error setting Message configuraton field "SMTPAuthenticate".' Goto ErrMssg End
EXEC @iHr = sp_OAMethod @iMessageObjId, 'Configuration.Fields.Update' IF @iHr <> 0 Begin Set @ErrMssg = 'Error updating Message configuration fields.' Goto ErrMssg End
EXEC @iHr = sp_OAMethod @iMessageObjId, 'Fields.Update' IF @iHr <> 0 Begin Set @ErrMssg = 'Error updating Message parameters.' Goto ErrMssg End
EXEC @iHr = sp_OAMethod @iMessageObjId, 'Send' IF @iHr <> 0 Begin Set @ErrMssg = 'Error Sending e-mail.' Goto ErrMssg End Else Print 'Mail sent.'
Cleanup: EXEC @iHr = sp_OADestroy @iMessageObjId
If @iFileExists = 1 Begin Select @Cmd = 'del ' + @QueryOutPath Exec master.dbo.xp_cmdshell @Cmd, no_output End Return
ErrMssg: Begin Print @ErrMssg If @iHr <> 0 Begin EXEC sp_OAGetErrorInfo @iMessageObjId, @ErrSource Out, @ErrDescription Out Print @ErrSource Print @ErrDescription End
If @ErrMssg = 'Error creating object CDO.Message.' Return Else Goto Cleanup End
GO |