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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Sending email based on timestamp.

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-07-07 : 06:14:51
Sean writes "I would like to send an email (SMTP remote using CDO via a stored porcedure) based on a datetime entry in a table. e.g. an autmatic email goes out 24 hours after the insert (or update).

What is the best way to do this?

Is there a way to do this?

Any help would be apreciated.

Sean"

stephe40
Posting Yak Master

218 Posts

Posted - 2005-07-07 : 11:44:14
Here is a stored procedure to send emails with CDO.


SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


CREATE PROCEDURE [dbo].[sp_sendmail] @From varchar(100), @To varchar(100), @Subject varchar(100), @Body varchar(4000), @CC varchar(100) = null, @BCC varchar(100) = null
AS


declare @oMsg int
declare @oConf int
declare @resultCode int

EXEC @resultCode = sp_OACreate 'CDO.Message', @oMsg OUT

if @resultCode = 0
begin

EXEC @resultCode = sp_OASetProperty @oMsg, 'Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing").value', '2'
if @resultCode <> 0 return @resultCode

EXEC @resultCode = sp_OASetProperty @oMsg, 'Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver").value', 'your.smtp.server'
if @resultCode <> 0 return @resultCode

EXEC @resultCode = sp_OAMethod @oMsg, 'Configuration.Fields.Update'
if @resultCode <> 0 return @resultCode


EXEC @resultCode = sp_OASetProperty @oMsg, 'Subject', @Subject
if @resultCode <> 0 return @resultCode

EXEC @resultCode = sp_OASetProperty @oMsg, 'To', @To
if @resultCode <> 0 return @resultCode

EXEC @resultCode = sp_OASetProperty @oMsg, 'From',@From
if @resultCode <> 0 return @resultCode

EXEC @resultCode = sp_OASetProperty @oMsg, 'TextBody', @Body
if @resultCode <> 0 return @resultCode

EXEC @resultCode = sp_OASetProperty @oMsg, 'BCC',@BCC
if @resultCode <> 0 return @resultCode

EXEC @resultCode = sp_OASetProperty @oMsg, 'CC', @CC
if @resultCode <> 0 return @resultCode

EXEC @resultCode = sp_OAMethod @oMsg, 'Send', NULL
if @resultCode <> 0 return @resultCode

EXEC @resultCode = sp_OADestroy @oConf
EXEC @resultCode = sp_OADestroy @oMsg

end

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO



After this your going to have to write a stored procedure that checks your queue and calls the sp_send mail when you want to send a email. And then just set that stored procedure up to run on a schedule.

- Eric
Go to Top of Page

stephe40
Posting Yak Master

218 Posts

Posted - 2005-07-07 : 11:45:27
Ohh yeah, just an FYI, this is for Server 2003.

- Eric
Go to Top of Page
   

- Advertisement -