Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sardinka
Posting Yak Master
142 Posts |
Posted - 2004-07-26 : 11:40:32
|
| Here is my code for CDOMail. However when I try to send to attachemnt it will not work (working with one attachment only). Any idea why?CREATE PROCEDURE CDO_SendMail(@From varchar(255) = 'sql@msn.com',@To varchar(255), @Cc varchar(255) = '', @Bcc varchar(255) = '', @Subject varchar(255),@MessageFormat int = 0, -- default to HTML, 1 = text, 0 = html@Attachments varchar(8000) = null,@Message varchar(8000),@Priority int = 2, -- default to high, 1 = normal, 0 = low@cdoSendUsingPort char(1) = '2', -- 1 = local smtp service, 2 = remote smtp service@MailServer varchar(20) = 'server') as--Send Emaildeclare @CDO int, @OLEResult int, @Out int-- Create CDO.Message objectexecute @OLEResult = sp_OACreate 'CDO.Message', @CDO OUTif @OLEResult <> 0 print 'CDO.Message'-- Set CDO.Message configuration propertiesexecute @OLEResult = sp_OASetProperty @CDO, 'Configuration.fields ("http://schemas.microsoft.com/cdo/configuration/sendusing"). Value', @cdoSendUsingPortexecute @OLEResult = sp_OASetProperty @CDO, 'Configuration.fields ("http://schemas.microsoft.com/cdo/configuration/smtpserver"). Value', @MailServer -- Save the configurations to the message object.execute @OLEResult = sp_OAMethod @CDO, 'Configuration.Fields.Update', Null-- Set CDO.Message propertiesexecute @OLEResult = sp_OASetProperty @CDO, 'From', @Fromexecute @OLEResult = sp_OASetProperty @CDO, 'To', @Toexecute @OLEResult = sp_OASetProperty @CDO, 'Cc', @Ccexecute @OLEResult = sp_OASetProperty @CDO, 'Bcc', @Bccexecute @OLEResult = sp_OASetProperty @CDO, 'Subject', @Subjectexecute @OLEResult = sp_OASetProperty @CDO, 'TextBody', @Messageexecute @OLEResult = sp_OASetProperty @CDO, 'Importance', @Priority--Added to handle attachmentsDeclare @files table(fileid int identity(1,1),[file] varchar(255))Declare @file varchar(255)Declare @filecount int ; set @filecount=0Declare @counter int ; set @counter = 1IF @attachments IS NOT NULLBEGININSERT @files SELECT value FROM dbo.fn_split(@attachments,',')SELECT @filecount=@@ROWCOUNTWHILE @counter<(@filecount+1)BEGINSELECT @file = [file] FROM @filesWHERE fileid=@counterEXEC @OLEResult = sp_OAMethod @CDO, 'AddAttachment', NULL, @attachmentsSET @counter=@counter+1ENDEND -- Call Send method of the objectexecute @OLEResult = sp_OAMethod @CDO, 'Send', Nulldeclare @source varchar(255), @description varchar(500), @output varchar(1000)if @OLEResult <> 0 beginexecute @OLEResult = sp_OAGetErrorInfo Null, @source OUT, @description OUTif @OLEResult = 0beginselect @output = ' Source: ' + @sourceprint @outputselect @output = ' Description: ' + @descriptionprint @outputendelsebeginprint ' sp_OAGetErrorInfo failed.'returnendend--Destroy CDOexecute @OLEResult = sp_OADestroy @CDOreturn @OLEResultGO |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-07-26 : 19:22:20
|
| Why don't you put the functions in here also so we can try it out. :)MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
sardinka
Posting Yak Master
142 Posts |
Posted - 2004-07-27 : 08:59:57
|
| CREATE FUNCTION fn_Split(@sText varchar(8000), @sDelim varchar(20) = ' ')RETURNS @retArray TABLE (idx smallint Primary Key, value varchar(8000))ASBEGINDECLARE @idx smallint, @value varchar(8000), @bcontinue bit, @iStrike smallint, @iDelimlength tinyintIF @sDelim = 'Space' BEGIN SET @sDelim = ' ' ENDSET @idx = 0SET @sText = LTrim(RTrim(@sText))SET @iDelimlength = DATALENGTH(@sDelim)SET @bcontinue = 1IF NOT ((@iDelimlength = 0) or (@sDelim = 'Empty')) BEGIN WHILE @bcontinue = 1 BEGIN--If you can find the delimiter in the text, retrieve the first element and--insert it with its index into the return table. IF CHARINDEX(@sDelim, @sText)>0 BEGIN SET @value = SUBSTRING(@sText,1, CHARINDEX(@sDelim,@sText)-1) BEGIN INSERT @retArray (idx, value) VALUES (@idx, @value) END --Trim the element and its delimiter from the front of the string. --Increment the index and loop.SET @iStrike = DATALENGTH(@value) + @iDelimlength SET @idx = @idx + 1 SET @sText = LTrim(Right(@sText,DATALENGTH(@sText) - @iStrike)) END ELSE BEGIN--If you can¨ˆt find the delimiter in the text, @sText is the last value in--@retArray. SET @value = @sText BEGIN INSERT @retArray (idx, value) VALUES (@idx, @value) END --Exit the WHILE loop.SET @bcontinue = 0 END END ENDELSE BEGIN WHILE @bcontinue=1 BEGIN --If the delimiter is an empty string, check for remaining text --instead of a delimiter. Insert the first character into the --retArray table. Trim the character from the front of the string.--Increment the index and loop. IF DATALENGTH(@sText)>1 BEGIN SET @value = SUBSTRING(@sText,1,1) BEGIN INSERT @retArray (idx, value) VALUES (@idx, @value) END SET @idx = @idx+1 SET @sText = SUBSTRING(@sText,2,DATALENGTH(@sText)-1) END ELSE BEGIN --One character remains. --Insert the character, and exit the WHILE loop. INSERT @retArray (idx, value) VALUES (@idx, @sText) SET @bcontinue = 0 END ENDENDRETURNEND |
 |
|
|
|
|
|
|
|