Email Scheduler

ramuis78

New member
Joined
Jul 14, 2006
Messages
3
Programming Experience
Beginner
I am working on email scheduler. The user would be able to schedule emails at certain times to run through the day,week,month with time. Scheduler continuously run in the system and sends email alerts for that time.
Any ideas how to send emails for the exact time.
for example:
Email1 should go at 7/15/2006 9:01:00 AM
Email2 should go at 7/15/2006 10:01:00 AM
Email3 should go at 7/15/2006 10:01:00 AM
Email4 should go at 7/15/2006 10:03:00 AM
Email5 should go at 7/15/2006 10:04:00 AM
Email6 should go at 7/15/2006 10:06:00 AM
Email7 should go at 7/15/2006 10:10:00 AM
Email8 should go at 7/15/2006 10:24:00 AM
Any help grealy appreicated.
Thanks
 
ramuis78 said:
I am working on email scheduler. The user would be able to schedule emails at certain times to run through the day,week,month with time. Scheduler continuously run in the system and sends email alerts for that time.
Any ideas how to send emails for the exact time.
for example:
Email1 should go at 7/15/2006 9:01:00 AM
Email2 should go at 7/15/2006 10:01:00 AM
Email3 should go at 7/15/2006 10:01:00 AM
Email4 should go at 7/15/2006 10:03:00 AM
Email5 should go at 7/15/2006 10:04:00 AM
Email6 should go at 7/15/2006 10:06:00 AM
Email7 should go at 7/15/2006 10:10:00 AM
Email8 should go at 7/15/2006 10:24:00 AM
Any help grealy appreicated.
Thanks
This is typically done using a Windows Service.
 
Use of Timer

Using Timer can cause you difficulties if your not careful. The best design as listed is the use of a windows service (specifically scheduler). I personally avoid the use of timer where possible, it usually is the source of more headaches than problems solved, but that is opinion speaking.... The manner that I would choose to handle this problem (assuming that the Windows Schedular and the Timer were NOT options) would be to create my own windows service. In the onStart event spawn a thread that will check the databases (or whatever your using for storing the pending emails) for emails to be sent in the next 2 hours and store those emails to an in-memory collection or array, then put this thread to sleep for 1 hour 59 minutes. Spawn an additional thread that will at specific intervals check for an email to be sent from the array/collection that was filled by the previous thread. This interval is dynamic and the thread wakes itself up just in time to send the email and then reset the sleep interval until the next email is to be sent. In that manner you are using the fewest resources possible. Put into place a refresh event that cause the whole load cycle to reinitialize so that you can react to the event of your email schedule for the next two hours changing... since users are prone to changing their minds. I arbitrarily picked out 2 hours, you can use whatever value you want, but the key is that between emails you want your thread to sleep. In that manner you are not constantly using resources to check was time it is and then send an email if its time such as is the case with the timer control.

Even if the schedule is as tight as you indicate in your initial post where there are mere seconds between emails, the timer set to 990 ms (or 1000 whichever) will fire 14 or so times before finding another time to send an email, those additional code runs are wasted rounds. However, if you want something that is just quick, I don;t care what resources I use implementation, the timer is the way to go, but you might still want to set your interval dynamically based on the time to the next email immediatly ahead, this would save a few iterations of your decision block.

If you want clarification on any point, let me know and I will do my best.
Cheers.
 
Thanks for your quick response.

I have implemented this using timers and it is working good. Windows Service is good as you said, but due to time frame and emails time frame i feel timer is suitable for my project and i run this 4 days back and till now i didn't get any probelm and it is sending emails timely.

Appreicate your great help.

Thanks
Ram
 
I am new to .net development, but am trying to do exactly the same thing you have mentioned in this post. do you mind posting some sample code to get me started?
 
what sample code you after?

What sample code are you after? Is it timer code? or is it code that is thread based?
 
sorry...i am not entirely sure what i am looking for. i just know what i want my end result to be, and am looking for a place to start. I am trying to set something up where i can keep a repository of multiple email addresses, and then multiple send schedules for each email address. i've already gotten the code figured out that will actually send the emails...i just dont know how to schedule them. for example:

email@email.com --> send @ 1:00, 1:05, 1:06
email2@email.com --> send @1:00, 2:00, 2:01
email3@email.com --> send @ 10:23, 11:00, 11:59, 12:00

any tips?
 
Example Code

ok, what you need is not a problem. Given your stated abilities I would not recommend anything overly complex. I have written a small piece of code that might do what you wish. Please note that I have set the properties of the control to only fire once every minute so only emails that are outstanding at the time the event fires are sent. You will need to figure out a couple of things based on this code. If you have no idea where to start, I might be able to help some but I am only in front of my computer for another 5 minutes.

VB.NET:
Imports System.Net.Mail
Public Class frmMain

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'set the timer's interval property to 60000 (this is approx 1 minute)
        Try
            'get the email info you want to send (you write the GetMailMessage routine)
            Dim MailMessage As MailMessage = GetMailMessage(Date.Now)
            'use your send code here that you indicated you already figured out
            'i am using the updated system.net.mail namespace as listed in the imports so if your
            'code that you figured out is for the old email namespace it will not work with this one.
            'if that is the case let me know and I wll find one of my working examples for you.
        Catch ex As Exception
            'either log the error or display a messagebox.  if you display a message box, you will pause your timer until it is 
            'answered by the user and you might not want that behavior.
        End Try
    End Sub

    Private Function GetMailMessage(ByVal CurrentTime As Date) As MailMessage
        Dim ReturnValue As MailMessage
        Try
            'get the info from your database and set the properties appropriately
            Dim rs As New DataSet  '= GetMailMessageFromDB(CurrentTime)
            If rs.Tables.Count > 0 Then
                If rs.Tables(0).Rows.Count > 0 Then
                    ReturnValue = New MailMessage("from@fromemai.com", "to@toemail.com")
                    ReturnValue.IsBodyHtml = True
                    ReturnValue.Subject = "get my subject from the data store"
                    ReturnValue.Body = "<strong>This is the body of the email, if you do use html " & _
                    "email, you want to lookup composing html email on the web before " & _
                    "doing so because there are tags that are not included in html email.</strong>"
                Else
                    Throw New Exception("no data returned")
                End If
            Else
                Throw New Exception("no data returned")
            End If
        Catch ex As Exception
            Throw ex
        End Try
        Return ReturnValue
    End Function
End Class

This code will send a single email on every iteration as long as you get information from the datastore... if you don't want to send an email on a given loop of the procedure then just ensure that the information coming back from the datastore is empty and an exception will be thrown and caught and the loop will continue on as before..
I have set this procedure to require that you write the GetMailMessageFromDb routine. I do not know where you are getting the info to send the emails from but I assume a database and my code demonstrates the return of the dataset (although I did not use it in the example you will find many examples on how to extract a row and use it from a dataset).
 
Last edited:
Back
Top