Domnic
Domnic

Reputation: 3867

Trigger mail automatically in ASP.NET

I have developed a .NET application with a SQL database. In SQL I have table Table_dom which contains a Date column where I will store date. Another table is Table_TL where I have one email column.

Now when the date is 5 days after the current date I need to send a reminder mail to TL from the Table_TL table email column. Could you please help on this?

Thank you.

Upvotes: 2

Views: 1930

Answers (1)

PHeiberg
PHeiberg

Reputation: 29811

If using SQL Server 2005 or later you could use the Database Mail feature to send emails directly from the database. A periodic check for sending reminders could be setup using a SQL Server Agent job.

Edit:

Provided you have setup the Database Mail correctly you should be able to add something like this (untested) to a SQL job:

DECLARE @recipient AS varchar(255)   
DECLARE users CURSOR FAST_FORWARD READ_ONLY FOR
    SELECT tl.email 
    FROM Table_TL tl 
    INNER JOIN Table_dom dom ON tl.thekey = dom.thekey
    WHERE DATEDIFF(day, dom.date, GETDATE()) > 5

OPEN users
FETCH NEXT FROM users INTO @recipient
WHILE @@FETCH_STATUS = 0
BEGIN
    EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'The profile you set up for the mail feature'
      , @recipients = @recipient
      , @subject = 'The subject'
      , @body = 'The message'

    FETCH NEXT FROM users INTO @recipient
END
CLOSE users 
DEALLOCATE users 

Upvotes: 1

Related Questions