Multiple Recipients

  • i mean ALZDBA's cursor solution does not work, i need another solution that works. I am sure many of us receive newsletters and we don't see other recipients email addresses. I need a procedure that will work for such a solution.

    Thank you.

  • Here is the actual proc we use.

    To send without recepients, set the parameter to '' (empty string) in stead of NULL.

    You need to adjust these parameters in the proc !!

    Ask for the correct parameters at your mailadmins.

    set @vcSMTPServer = 'dummymailalias' -- onze virtuele SMTP mail server op ALZ

    set @cSendUsing = '2'-- Specifies the smpt server method, local or network. The default is network, a value of '2'

    set @vcPort = '25' -- smtp poort

    set @cAuthenticate = '0'-- The smtp server authentication method defaulted to anonymous, a value of '0'.

    set @vcDSNOptions = '0'-- The smtp server delivery status defaulted to none, a value of '0'

    set @vcTimeout = '30'-- The smtp server connection timeout defaulted to 30 seconds

    set @vcSenderName = null-- Primary sender name defaulted to @@ServerName

    use master

    go

    if exists (select *

    from sysobjects

    where id = object_id(N'[dbo].[sp_ALZDBA_SQLSMTPMail]')

    and OBJECTPROPERTY(id, N'IsProcedure') = 1)

    begin

    drop procedure [dbo].[sp_ALZDBA_SQLSMTPMail]

    print 'Procedure [sp_ALZDBA_SQLSMTPMail] dropped.'

    end

    go

    CREATE Procedure dbo.sp_ALZDBA_SQLSMTPMail

    @recipients varchar(2048) = null,

    @message varchar(8000) = '',

    @query varchar(7000) = null,

    @attachments varchar(1024) = null,

    @copy_recipients varchar(2048) = '',

    @blind_copy_recipients varchar(2048) = '',

    @subject varchar(255) = null,

    @width integer = null,

    @separator char(1) = null,

    @Headers integer = 0,

    @attach_resultsYN char(1) = 'Y',

    @iQueryResultName varchar(128) = 'TempALZQueryOut',

    @iQueryResultExtention varchar(3) = 'TXT',

    @iFrom varchar(128) = null

    -- with encryption

    As

    /*******************************************************************/

    --Name : sp_ALZDBA_SQLSMTPMail

    --Server : Generic

    --Description : SQL smtp e-mail using CDOSYS, OLE Automation and a

    -- network smtp server; For SQL Servers running on

    -- windows 2000.

    --

    --Note : Be sure to set the default for @vcSMTPServer above to

    -- the company network smtp server or you will have to

    -- pass it in each time.

    --

    --Comments : Getting the network SMTP configured to work properly

    -- may require engaging your company network or

    -- server people who deal Width the netowrk SMTP server.

    -- Some errors that the stored proc returns relate to

    -- incorrect permissions for the various SQL Servers to

    -- use the SMTP relay server to bouce out going mail.

    -- Widthout proper permissions the SQL server appears as

    -- a spammer to the local SMTP network server.

    --

    --Parameters : See the 'Syntax' Print statements below or call the

    -- sp Width '?' as the first input.

    --

    --Date : 08/22/2001

    --Author : Clinton Herring

    --

    --History : JOBI dd 04/06/2003 aanpassing voor vervanging van xp_Sendmail.

    -- xp_Sendmail paramaters die niet geïmplementeerd werden

    -- [,[@type =] 'type'] -- stond toch overal op NULL

    -- [,[@attach_results =] 'attach_value'] -- true/false

    -- [,[@no_output =] 'output_value'] -- true/false

    -- [,[@no_header =] 'header_value'] -- true/false

    -- [,[@echo_error =] 'echo_value']

    -- [,[@set_user =] 'user']

    -- [,[@dbuse =] 'database']

    --

    /*******************************************************************/

    Set nocount on

    -- dit zijn de oorspronkelijke smtpmail parameters de interface-lijst werd aangepast voor makkelijke overschakeling van xp_sendmail naar sp_

    Declare @vcTo varchar(2048)

    Declare @vcBody varchar(8000)

    Declare @vcSubject varchar(255)

    Declare @vcAttachments varchar(1024)

    Declare @vcQuery varchar(7000)

    Declare @vcFrom varchar(128)

    Declare @vcCC varchar(2048)

    Declare @vcBCC varchar(2048)

    Declare @vcSMTPServer varchar(255)

    Declare @cSendUsing char(1)

    Declare @vcPort varchar(3)

    Declare @cAuthenticate char(1)

    Declare @vcDSNOptions varchar(2)

    Declare @vcTimeout varchar(2)

    Declare @vcSenderName varchar(128)

    Declare @vcServerName sysname

    Declare @vcWidth integer

    Declare @vcSeparator char(1)

    Declare @vcHeader varchar(2)

    Declare @tmpTbGuid varchar(50), @tmpTbCreate varchar(250), @tmpTbDrop varchar(250)

    --initialisatie sys-parms en overname van interfacegegevens

    Set @vcTo = @recipients

    Set @vcBody = @message

    Set @vcQuery = @query

    Set @vcAttachments = @attachments

    Set @vcCC = @copy_recipients

    Set @vcBCC = @blind_copy_recipients

    Set @vcSubject = @subject

    set @vcFrom = @iFrom

    Select @vcWidth = isnull(@width, 5000) -- indien null default van 5000 nemen

    Select @vcSeparator = isnull(@separator,' ') -- indien NULL default een blanco nemen

    if @iQueryResultName = ''

    begin

    set @iQueryResultName = 'TempALZQueryOut'

    end

    if coalesce(@iQueryResultExtention,'') = ''

    begin

    set @iQueryResultExtention = 'TXT'

    end

    set @vcSMTPServer = 'dummymailalias' -- onze virtuele SMTP mail server op ALZ

    set @cSendUsing = '2'-- Specifies the smpt server method, local or network. The default is network, a value of '2'

    set @vcPort = '25' -- smtp poort

    set @cAuthenticate = '0'-- The smtp server authentication method defaulted to anonymous, a value of '0'.

    set @vcDSNOptions = '0'-- The smtp server delivery status defaulted to none, a value of '0'

    set @vcTimeout = '30'-- The smtp server connection timeout defaulted to 30 seconds

    set @vcSenderName = null-- Primary sender name defaulted to @@ServerName

    -- Determine if the user requested syntax.

    If @vcTo = '?'

    Begin

    Print 'Syntax for sp_ALZDBA_SQLSMTPMail (based on CDOSYS):'

    Print 'Exec master.dbo.sp_ALZDBA_SQLSMTPMail'

    Print ' @recipients (varchar(2048)) - Recipient e-mail address list separating each Width a '';'' '

    Print ' or a '',''. Use a ''?'' to return the syntax.'

    Print ' @message (varchar(8000)) - Text body; use embedded char(13) + char(10)'

    Print ' for carriage returns. The default is nothing'

    Print ' @Query (varchar(8000)) - In-line query or a query file path; do not '

    Print ' use double quotes Widthin the query.'

    Print ' @Attachments (varchar(1024)) - Attachment list separating each Width a '';''.'

    Print ' The default is no attachments.'

    Print ' @copy_recipients (varchar(2048)) - CC list separating each Width a '';'' or a '','''

    Print ' The default is no CC addresses.'

    Print ' @blind_copy_recipients(varchar(2048)) - Blind CC list separating each Width a '';'' or a '','''

    Print ' The default is no BCC addresses.'

    Print ' @subject (varchar(255))) - E-mail subject. The default is a message from'

    Print ' @@servername.'

    Print ' @width (integer) - rowwidth when queryresult is to be returned'

    Print ' @separator (char(1)) - used to separate columns when executing a query'

    Print ' @Headers (integer) - -1 = no-headers '

    Print ' @attach_resultsYN (char(1)) - attach query-results Y / N'

    Print ' @iQueryResultName varchar(128) - prefix-filename voor query-result & attatchment'

    Print ' @iQueryResultExtention varchar(3) - default = TXT'

    Print ' @iFrom (varchar(128)) - Sender list defaulted to @@ServerName.'

    Print ''

    Return

    End

    -- Declare variables

    Declare @iMessageObjId int

    Declare @iHr int

    Declare @iRtn int

    Declare @iFileExists tinyint

    Declare @vcCmd varchar(8000)

    Declare @vcQueryOutPath varchar(255)

    Declare @dtDatetime datetime

    Declare @vcErrMssg varchar(255)

    Declare @vcAttachment varchar(1024)

    Declare @iPos int

    Declare @vcErrSource varchar(255)

    Declare @vcErrDescription varchar(255)

    -- Set local variables.

    Select @dtDatetime = getdate()

    , @iHr = 0

    , @vcErrMssg = ''

    -- Check for minimum parameters.

    If @vcTo is null

    Begin

    Set @vcErrMssg = 'You must supply at least 1 recipient.'

    Goto ErrMssg

    End

    If isnull(@vcQuery,'') <> ''

    Begin

    declare @BevatDeleteStmt integer

    Set @BevatDeleteStmt = 999

    Select @BevatDeleteStmt = case

    when PATINDEX('Delete %', @vcQuery) > 0 then 1

    when PATINDEX('%(Delete %', @vcQuery) > 0 then 2

    when PATINDEX('% Delete %', @vcQuery) > 0 then 3

    else 0

    end

    if @BevatDeleteStmt > 0

    begin

    Set @vcErrMssg = 'Delete-statements not allowed.(ALZ)'

    Goto ErrMssg

    end

    if datalength(@vcQuery) > 1000

    begin

    declare @tmpTbIsrt varchar(8000)

    select @tmpTbGuid = newid()

    select @tmpTbCreate = 'Create table [##ALZSMTPMail' + @tmpTbGuid + '] ( SQLText varchar(8000) not null ) '

    , @tmpTbDrop = 'Drop table [##ALZSMTPMail' + @tmpTbGuid + '] '

    , @tmpTbIsrt = 'Insert into [##ALZSMTPMail' + @tmpTbGuid + '] values (''' + replace(@vcQuery, '''', '''''') + ''')'

    exec (@tmpTbCreate)

    exec (@tmpTbIsrt)

    select @vcQuery = 'declare @SqlTxt as varchar(8000)

    Select top 1 @SqlTxt = SQLText from [##ALZSMTPMail' + @tmpTbGuid + ']

    exec (@SqlTxt)'

    --Set @vcErrMssg = 'Querytext is too long.(ALZ)'

    --Goto ErrMssg

    end

    End

    -- CDOSYS uses commas to separate recipients. Allow users to use

    -- either a comma or a semi-colon by replacing semi-colons in the

    -- To, CCs and BCCs.

    Select @vcTo = Replace(@vcTo, ';', ',')

    Select @vcCC = Replace(@vcCC, ';', ',')

    Select @vcBCC = Replace(@vcBCC, ';', ',')

    -- Set the default SQL Server to the local SQL Server if one

    -- is not provided to accommodate instances in SQL 2000.

    If @vcServerName is null

    Set @vcServerName = @@servername

    -- Set a default "subject" if one is not provided.

    If @vcSubject is null

    Set @vcSubject = 'Message from SQL Server ' + @vcServerName

    -- Set a default "from" if one is not provided.

    If @vcFrom is null

    Set @vcFrom = 'ALZDBA_Belgium-SQL-' + Replace(@vcServerName,'\','_')

    -- Set a default "sender name" if one is not provided.

    If @vcSenderName is null

    Set @vcSenderName = 'ALZDBA_Belgium-SQL-' + Replace(@vcServerName,'\','_')

    -- Create the SMTP message object.

    EXEC @iHr = sp_OACreate 'CDO.Message', @iMessageObjId OUT

    IF @iHr <> 0

    Begin

    Set @vcErrMssg = 'Error creating object CDO.Message.'

    Goto ErrMssg

    End

    -- Set SMTP message object parameters.

    -- To

    EXEC @iHr = sp_OASetProperty @iMessageObjId, 'To', @vcTo

    IF @iHr <> 0

    Begin

    Set @vcErrMssg = 'Error setting Message parameter "To".'

    Goto ErrMssg

    End

    -- Subject

    EXEC @iHr = sp_OASetProperty @iMessageObjId, 'Subject', @vcSubject

    IF @iHr <> 0

    Begin

    Set @vcErrMssg = 'Error setting Message parameter "Subject".'

    Goto ErrMssg

    End

    -- From

    EXEC @iHr = sp_OASetProperty @iMessageObjId, 'From', @vcFrom

    IF @iHr <> 0

    Begin

    Set @vcErrMssg = 'Error setting Message parameter "From".'

    Goto ErrMssg

    End

    -- CC

    EXEC @iHr = sp_OASetProperty @iMessageObjId, 'CC', @vcCC

    IF @iHr <> 0

    Begin

    Set @vcErrMssg = 'Error setting Message parameter "CC".'

    Goto ErrMssg

    End

    -- BCC

    EXEC @iHr = sp_OASetProperty @iMessageObjId, 'BCC', @vcBCC

    IF @iHr <> 0

    Begin

    Set @vcErrMssg = 'Error setting Message parameter "BCC".'

    Goto ErrMssg

    End

    -- DSNOptions

    EXEC @iHr = sp_OASetProperty @iMessageObjId, 'DSNOptions', @vcDSNOptions

    IF @iHr <> 0

    Begin

    Set @vcErrMssg = 'Error setting Message parameter "DSNOptions".'

    Goto ErrMssg

    End

    -- Sender

    EXEC @iHr = sp_OASetProperty @iMessageObjId, 'Sender', @vcSenderName

    IF @iHr <> 0

    Begin

    Set @vcErrMssg = 'Error setting Message parameter "Sender".'

    Goto ErrMssg

    End

    -- Is there a query to run?

    If @vcQuery is not null and @vcQuery <> ''

    Begin

    --remove line feed and Carriage return characters (OSQL does not function when they are included)

    SELECT @vcQuery = REPLACE(@vcQuery,char(10),' ') --JOBI

    SELECT @vcQuery = REPLACE(@vcQuery,char(13),' ') --JOBI

    -- We have a query result to include; temporarily send the output to the

    -- drive Width the most free space. Use xp_fixeddrives to determine this.

    -- If a temp table exists Width the following name drop it.

    If (Select object_id('tempdb.dbo.#fixeddrives')) > 0

    Exec ('Drop table #fixeddrives')

    -- Create a temp table to work Width xp_fixeddrives.

    Create table #fixeddrives(

    Drive char(1) null,

    FreeSpace varchar(15) null)

    -- Get the fixeddrive info.

    Insert into #fixeddrives Exec master.dbo.xp_fixeddrives

    -- Get the drive letter of the drive Width the most free space

    -- Note: The OSQL output file name must be unique for each call Widthin the same session.

    -- Apparently OSQL does not release its lock on the first file created until the session ends.

    -- Hence this alleviates a problem Width queries from multiple calls in a cursor or other loop.

    Select @vcQueryOutPath = Drive + ':\' + @iQueryResultName + '_' +

    ltrim(str(datepart(hh,getdate()))) +

    ltrim(str(datepart(mi,getdate()))) +

    ltrim(str(datepart(ss,getdate()))) +

    ltrim(str(datepart(ms,getdate()))) + '.' + @iQueryResultExtention

    from #fixeddrives

    where FreeSpace = (select max(FreeSpace) from #fixeddrives )

    -- Check for a pattern of '\\*\' or '?:\'.

    -- If found assume the query is a file path.

    If Left(@vcQuery, 35) like '\\%\%' or Left(@vcQuery, 5) like '_:\%'

    Begin

    Select @vcCmd = 'osql /S' + @vcServerName + ' /E /i' + convert(varchar(1024),@vcQuery) +

    case @attach_resultsYN when 'Y' then '-o' + @vcQueryOutPath else '' end +

    ' -n -w' + ltrim(convert(varchar(6),@vcWidth)) +

    ' -s"' + @vcSeparator + '"' +

    ' -h' + ltrim(convert(varchar(6),@Headers))

    End

    Else

    Begin

    Select @vcCmd = 'osql /S' + @vcServerName + ' /E /Q"set nocount on '+ char(13) + @vcQuery + '" ' +

    case @attach_resultsYN when 'Y' then '-o' + @vcQueryOutPath else '' end +

    ' -n -w' + ltrim(convert(varchar(6),@vcWidth)) +

    ' -s"' + @vcSeparator + '"' +

    ' -h' + ltrim(convert(varchar(6),@Headers))

    End

    -- PRINT '@vcCmd [' + @vcCmd + ']' --JOBI

    -- Execute the query

    Exec master.dbo.xp_cmdshell @vcCmd, no_output

    -- Add the query results as an attachment if the file was successfully created.

    -- Check to see if the file exists. Use xp_fileexist to determine this.

    -- If a temp table exists Width the following name drop it.

    If (Select object_id('tempdb.dbo.#fileexists')) > 0

    Exec ('Drop table #fileexists')

    -- Create a temp table to work Width xp_fileexist.

    Create table #fileexists(

    FileExists tinyint null,

    FileIsDirectory tinyint null,

    ParentDirectoryExists tinyint null)

    -- Execute xp_fileexist

    Insert into #fileexists exec master.dbo.xp_fileexist @vcQueryOutPath

    -- Now see if we need to add the file as an attachment

    If (select FileExists from #fileexists) = 1

    Begin

    -- Set a variable for later use to delete the file.

    Select @iFileExists = 1

    -- Add the file path to the attachment variable.

    If @vcAttachments is null

    Select @vcAttachments = @vcQueryOutPath

    Else

    Select @vcAttachments = @vcAttachments + '; ' + @vcQueryOutPath

    End

    else

    Begin

    Select @vcBody = @vcBody + char(13) + char(13) + '*** Query Returned No Results - No Results attached ***'

    End

    End

    -- Check for multiple attachments separated by a semi-colon ';'.

    If @vcAttachments is not null

    Begin

    If right(@vcAttachments,1) <> ';'

    Select @vcAttachments = @vcAttachments + '; '

    Select @iPos = CharIndex(';', @vcAttachments, 1)

    While @iPos > 0

    Begin

    Select @vcAttachment = ltrim(rtrim(substring(@vcAttachments, 1, @iPos -1)))

    Select @vcAttachments = substring(@vcAttachments, @iPos + 1, Len(@vcAttachments)-@iPos)

    EXEC @iHr = sp_OAMethod @iMessageObjId, 'AddAttachment', @iRtn Out, @vcAttachment

    IF @iHr <> 0

    Begin

    EXEC sp_OAGetErrorInfo @iMessageObjId, @vcErrSource Out, @vcErrDescription Out

    Select @vcBody = @vcBody + char(13) + char(10) + char(13) + char(10) +

    char(13) + char(10) + 'Error adding attachment: ' +

    char(13) + char(10) + @vcErrSource + char(13) + char(10) +

    @vcAttachment

    End

    Select @iPos = CharIndex(';', @vcAttachments, 1)

    End

    End

    -- TextBody

    EXEC @iHr = sp_OASetProperty @iMessageObjId, 'TextBody', @vcBody

    IF @iHr <> 0

    Begin

    Set @vcErrMssg = 'Error setting Message parameter "TextBody".'

    Goto ErrMssg

    End

    -- Other Message parameters for reference

    --EXEC @iHr = sp_OASetProperty @iMessageObjId, 'MimeFormatted', False

    --EXEC @iHr = sp_OASetProperty @iMessageObjId, 'AutoGenerateTextBody', False

    --EXEC @iHr = sp_OASetProperty @iMessageObjId, 'MDNRequested', True

    -- Set SMTP Message configuration property values.

    -- Network SMTP Server location

    EXEC @iHr = sp_OASetProperty @iMessageObjId,

    'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value',

    @vcSMTPServer

    IF @iHr <> 0

    Begin

    Set @vcErrMssg = 'Error setting Message configuraton field "smtpserver".'

    Goto ErrMssg

    End

    -- Sendusing

    EXEC @iHr = sp_OASetProperty @iMessageObjId,

    'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value',

    @cSendUsing

    IF @iHr <> 0

    Begin

    Set @vcErrMssg = 'Error setting Message configuraton field "sendusing".'

    Goto ErrMssg

    End

    -- SMTPConnectionTimeout

    EXEC @iHr = sp_OASetProperty @iMessageObjId,

    'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/SMTPConnectionTimeout").Value',

    @vcTimeout

    IF @iHr <> 0

    Begin

    Set @vcErrMssg = 'Error setting Message configuraton field "SMTPConnectionTimeout".'

    Goto ErrMssg

    End

    -- SMTPServerPort

    EXEC @iHr = sp_OASetProperty @iMessageObjId,

    'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/SMTPServerPort").Value',

    @vcPort

    IF @iHr <> 0

    Begin

    Set @vcErrMssg = 'Error setting Message configuraton field "SMTPServerPort".'

    Goto ErrMssg

    End

    -- SMTPAuthenticate

    EXEC @iHr = sp_OASetProperty @iMessageObjId,

    'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/SMTPAuthenticate").Value',

    @cAuthenticate

    IF @iHr <> 0

    Begin

    Set @vcErrMssg = 'Error setting Message configuraton field "SMTPAuthenticate".'

    Goto ErrMssg

    End

    -- Other Message Configuration fields for reference

    --EXEC @iHr = sp_OASetProperty @iMessageObjId,

    --'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/SMTPUseSSL").Value',True

    --EXEC @iHr = sp_OASetProperty @iMessageObjId,

    --'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/LanguageCode").Value','en'

    --EXEC @iHr = sp_OASetProperty @iMessageObjId,

    --'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/SendEmailAddress").Value', 'Test User'

    --EXEC @iHr = sp_OASetProperty @iMessageObjId,

    --'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/SendUserName").Value',null

    --EXEC @iHr = sp_OASetProperty @iMessageObjId,

    --'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/SendPassword").Value',null

    -- Update the Message object fields and configuration fields.

    EXEC @iHr = sp_OAMethod @iMessageObjId, 'Configuration.Fields.Update'

    IF @iHr <> 0

    Begin

    Set @vcErrMssg = 'Error updating Message configuration fields.'

    Goto ErrMssg

    End

    EXEC @iHr = sp_OAMethod @iMessageObjId, 'Fields.Update'

    IF @iHr <> 0

    Begin

    Set @vcErrMssg = 'Error updating Message parameters.'

    Goto ErrMssg

    End

    -- Send the message.

    EXEC @iHr = sp_OAMethod @iMessageObjId, 'Send'

    IF @iHr <> 0

    Begin

    Set @vcErrMssg = 'Error Sending e-mail.'

    Goto ErrMssg

    End

    Else

    Print 'Mail sent.'

    Cleanup:

    -- Destroy the object and return.

    EXEC @iHr = sp_OADestroy @iMessageObjId

    --EXEC @iHr = sp_OAStop

    if @tmpTbDrop is not null

    begin

    --print @tmpTbDrop

    exec (@tmpTbDrop)

    end

    -- Delete the query output file if one exists.

    If @iFileExists = 1

    Begin

    Select @vcCmd = 'del ' + @vcQueryOutPath

    Exec master.dbo.xp_cmdshell @vcCmd, no_output

    End

    if @vcErrMssg = ''

    begin

    Return(0)

    end

    else

    begin

    print '[' + @vcErrMssg + ']'

    Return(12)

    end

    ErrMssg:

    Begin

    Print @vcErrMssg

    If @iHr <> 0

    Begin

    EXEC sp_OAGetErrorInfo @iMessageObjId, @vcErrSource Out, @vcErrDescription Out

    Print @vcErrSource

    Print @vcErrDescription

    End

    -- Determine whether to exist or go to Cleanup.

    If @vcErrMssg = 'Error creating object CDO.Message.'

    Return

    Else

    Goto Cleanup

    End

    Go

    usage :

    exec dbo.sp_ALZ_SQLSMTPMail

    @recipients ='',

    @message = 'mail Origin = sqlserver - TEST mail',

    @subject = 'Onderwerp',

    @query = 'exec sp_who',

    @copy_recipients = null,

    @blind_copy_recipients = 'dummy.address@mydomain.com;another@mydomain.com',

    @width = null,

    @separator = ' ',

    @Headers = 0

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • adekunle (4/8/2008)


    i mean ALZDBA's cursor solution does not work, i need another solution that works. I am sure many of us receive newsletters and we don't see other recipients email addresses. I need a procedure that will work for such a solution.

    Thank you.

    In most cases these are sent by email campaign software such as campaign by http://www.arialsoftware.com or a component like jmail from http://www.dimac.net.

    You can use this in SQL.

    declare @toemail varchar(50)

    declare email_cursor cursor for select email from birthdays2

    open email_cursor

    fetch next from email_cursor

    into @toemail

    WHILE @@FETCH_STATUS = 0

    BEGIN

    print @toemail

    EXEC master.dbo.xp_smtp_sendmail

    @TO = @toemail,

    --@BCC = @BCCList,

    @from = 'birthdayclub@birthday.com', --change this

    @subject = 'Happy Birthday to you!',

    @message = 'You have a great future!',

    @server = 'smtp.yourdomain.net' --change this

    FETCH NEXT FROM email_cursor

    into @toemail

    END

    CLOSE email_cursor

    DEALLOCATE email_cursor


  • I want to say thank you to everybody that has contributed to this post expecially to ALZDBA unfortunately i can not use his solution, i want to thank mrpolecat his solution works for me, my question is will the variable @toemail be able to handle up to 100 email addresses and what impact will it have on perfomance.

    Thank you.

  • I'm glad you got it to work.

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • because we are using a cursor loop the @toemail variable is only holding one email address at a time so it will handle as many email addresses as you need. The limit is a length of 50 chracters for each email but you can change that if you need to.


Viewing 6 posts - 16 through 20 (of 20 total)

You must be logged in to reply to this topic. Login to reply