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 |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2006-08-11 : 08:49:49
|
| Maciek writes "Hi,I would like to have procedure which will be sending emails to addresses returned by query.Below procedure returns by function PRINT 2 values but it sends email just to one address.Do I use CURSOR correctly?Final query I will return table with row with details for variables,( each row have detail for 1 email) :-@From varchar(100) ,-@To varchar(100) ,-@Subject varchar(100)=" ",-@Body varchar(4000) =" ",CREATE PROCEDURE [dbo].[sp_send_cdosysmail2] @From varchar(100) ,--@To varchar(100) ,@Subject varchar(100)=" ",@Body varchar(4000) =" ",@server varchar(100)=" "--@username varchar (20)=" ",--@password varchar (20)=" "/*********************************************************************This stored procedure takes the parameters and sends an e-mail. All the mail configurations are hard-coded in the stored procedure. Comments are added to the stored procedure where necessary.References to the CDOSYS objects are at the following MSDN Web site:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_messaging.asp***********************************************************************/ASDeclare @To varchar(100) DECLARE c1 CURSOR FOR/***sample select which returns 2 values*****/SELECT TestUsr.adresFROM TPIncidentINNER JOIN TestUsrON TestUsr.id = TPIncident.TesterWHERE TPIncident.OwnedName = 'CR5073' OR TPIncident.OwnedName = 'CR5074'OPEN c1FETCH NEXT FROM c1INTO @ToWHILE @@FETCH_STATUS = 0BEGIN PRINT @To FETCH NEXT FROM c1 INTO @To ENDCLOSE c1DEALLOCATE c1Declare @iMsg intDeclare @hr intDeclare @source varchar(255)Declare @description varchar(500)Declare @output varchar(1000)--************* Create the CDO.Message Object ************************EXEC @hr = sp_OACreate 'CDO.Message', @iMsg OUT--***************Configuring the Message Object ******************-- This is to configure a remote SMTP server.--http://schemas.microsoft.com/cdo/configuration/sendusing-- http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_schema_configuration_sendusing.asp--1 for localhost--2 for remote SMTPEXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value','2'-- This is to configure the Server Name or IP address. -- Replace MailServerName by the name or IP of your SMTP ServerEXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', @serverEXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout").Value', 600 --cdoBasicEXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate").Value', 1 --cdoBasic--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', @passwordEXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/urlproxyserver").Value', '161.139.66.103:6588' --ourserver:portEXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/urlproxybypass").Value', '<local>' --if local IP, no need proxyEXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/urlgetlatestversion").Value', true-- EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fiel |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2006-08-11 : 08:50:39
|
| You might want to take a look at this article:http://www.sqlteam.com/item.asp?ItemID=5908 |
 |
|
|
|
|
|
|
|