App. to send email everyday at 4am

Aggie88

New member
Joined
Jun 9, 2010
Messages
1
Programming Experience
Beginner
Hey,

I'm trying to create a function that sends an email everyday at a specific time. I thought threading.timer would work but i haven't figured it out yet. I don't get errors when its executed but it also does nothing. If someone could tell me whats going on with this piece of code, i would greatly appreciate it. The program will be put in a windows scheduler.

Thanks

VB.NET:
Private Shared Sub Main(ByVal args As String())
            'get today's date at 9:00 AM
            Dim nineAM As DateTime = DateTime.Today.AddHours(11)

            'if 9:00AM has passed, get tomorrow at 9:00 AM
            If DateTime.Now > nineAM Then
                nineAM = nineAM.AddDays(1)
            End If

            ' calculate milliseconds until the next 9:00AM
            Dim FirstExecution As Integer = CInt(Math.Truncate(nineAM.Subtract(DateTime.Now).TotalMilliseconds))

            ' calculate the number of milliseconds in 24 hours
            Dim TimeSpan As Integer = CInt(Math.Truncate(New TimeSpan(0, 2, 0).TotalMilliseconds))

            'set the method to execute when the time executes
            Dim ExecuteEmail As TimerCallback = AddressOf Email

            'start the timer. The timer will execute "Email" when the number of seconds between now
            'the next 9:00 AM elapse. After that, it will execute every 24 hours
            Dim timer As New System.Threading.Timer(ExecuteEmail, Nothing, FirstExecution, TimeSpan)

            'Block the main thread forever. The timer will continue to execute
            Thread.Sleep(Timeout.Infinite)

        End Sub
 
You're using windows scheduler. Why don't you just set it to run every day at 9AM and have the program's only functionality be to email?
 
Hey,

I'm trying to create a function that sends an email everyday at a specific time. I thought threading.timer would work but i haven't figured it out yet. I don't get errors when its executed but it also does nothing. If someone could tell me whats going on with this piece of code, i would greatly appreciate it.
The logic of your program is all screwed up (and you didnt post the entire program.)

I'd definitely follow the other advice of just making your app send the mail when it is started and hand the job of scheduling off to windows task scheduler

Oh, and never use Thread.Sleep
never use thread.sleep - Google Search
 
Back
Top