locked
Send email every hour RRS feed

  • Question

  • User-1873346364 posted

    How could you send an email every hour from asp.net using visual basic?

    I want it so that it doesn't only send when a user requests a page on the site.

    Instead  asp needs a timer to send email every hour it on it's own

     

    Thanks 





     

    Wednesday, November 14, 2007 4:54 PM

Answers

  • User-627724879 posted

    I wrote a Blog entry on the basic mechanism I use to set a background thread in ASP.NET. It kicks off in the Application start event and will fire at the interval you specify. I show how to just store the current time to the text file. You need to take the mechnism and have it fire an e-mail according to your que.

    http://professionalaspnet.com/archive/2007/09/02/Creating-a-Background-Thread-When-the-Web-Site-First-Starts.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 14, 2007 5:55 PM
  • User-1873346364 posted

     I found the best way to do it is this is to make sure you add 'try' or else it  will keep looping exceptions causing high memory  overhead

     In your global asax:

     <%@ Import Namespace ="System.Threading" %>


      Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
                 
            Dim t As New Thread(AddressOf WriteLog)
            t.Start()

        End Sub

           Public Shared Sub WriteLog()


            Do

                Try
                     'Send email code here


                Catch ex As Exception
                End Try

     

    'pause for 10 seconds
                Thread.Sleep(10000)

            Loop


        End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 14, 2007 7:57 PM

All replies

  • User-627724879 posted

    I wrote a Blog entry on the basic mechanism I use to set a background thread in ASP.NET. It kicks off in the Application start event and will fire at the interval you specify. I show how to just store the current time to the text file. You need to take the mechnism and have it fire an e-mail according to your que.

    http://professionalaspnet.com/archive/2007/09/02/Creating-a-Background-Thread-When-the-Web-Site-First-Starts.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 14, 2007 5:55 PM
  • User-1873346364 posted

    Awesome stuff thanks!


    That's just the sort of thing I'm after.

     

     

     

     

    Wednesday, November 14, 2007 6:11 PM
  • User-1873346364 posted

    I tried that, but I find it just continuously loops ? Causing high CPU usage

    Any ideas?

     

    Wednesday, November 14, 2007 7:07 PM
  • User-128772008 posted

    Off the top of my head, two ideas come to mind

    1) sql server batch job

    2) create a service for it

    I'm only a little familiar with #1, but I've dealt with stored procedures that run at 2:00 AM each night, or 3:00 AM, etc. The stored procedures would also use sql server to send emails to individuals.

    I'm not sure what #2 would entail.

    Wednesday, November 14, 2007 7:32 PM
  • User-1873346364 posted

     I found the best way to do it is this is to make sure you add 'try' or else it  will keep looping exceptions causing high memory  overhead

     In your global asax:

     <%@ Import Namespace ="System.Threading" %>


      Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
                 
            Dim t As New Thread(AddressOf WriteLog)
            t.Start()

        End Sub

           Public Shared Sub WriteLog()


            Do

                Try
                     'Send email code here


                Catch ex As Exception
                End Try

     

    'pause for 10 seconds
                Thread.Sleep(10000)

            Loop


        End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 14, 2007 7:57 PM
  • User-694963578 posted

    You could write a console application and run it under scheduler every hour.  I find this a lot easier than trying to do it within ASP.NET.  Don't forget that if the application restarts, all your timers restart as well.  You need to add extra code to manage the timing in this scenario.  K.I.S.S.

    Thursday, November 15, 2007 11:31 AM
  • User-1055514074 posted

    I worked on a similar task where I had to send emails every 30 mins. I found writing a windows service was very easy and the best way to do it.

     

     

    Thursday, November 15, 2007 11:38 AM
  • User-2009115262 posted

    Can you walk me through your code? i want to develope same windows service to send email.

     

    Wednesday, April 2, 2008 1:45 AM