send mail using timer

Hello!

I have a tool which would run as a service in the background, and I want it to send a mail every day at, let's say 02:00am. How can I do this With a timer or something

Right now, the tool is working, it's parsing the xml file and get's the values from it that it needs to send the mail.

Here's the code:

XmlDocument xDoc = new XmlDocument();

xDoc.Load("settings_mail.xml");

// XmlNodeList days = xDoc.GetElementsByTagName("maildays");

// XmlNodeList times = xDoc.GetElementsByTagName("mailtimes");

XmlNodeList from = xDoc.GetElementsByTagName("origin");

XmlNodeList dest = xDoc.GetElementsByTagName("destination");

XmlNodeList host = xDoc.GetElementsByTagName("host");

XmlNodeList subj = xDoc.GetElementsByTagName("subject");

XmlNodeList mess = xDoc.GetElementsByTagName("message");

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment("test.xml");

message.Attachments.Add(attach);

message.To.Add( dest[0].InnerText );

message.Subject = subj[0].InnerText ;

message.Body = mess[0].InnerText ;

message.From = new System.Net.Mail.MailAddress( from[0].InnerText );

System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("fis0167.accdom.for.int");

smtp.Send(message);



Answer this question

send mail using timer

  • Machie

    Use a timer with a delay of 1 minute (pretty precise mailing but perhaps too heavy) or one hour (mailing somewhere around 02.00 am) or anything in between, that's up to you. You should think about how important it is to send the mail at the exact time...

    In the timer, check the time. If you're close (enough) to the time you would like to send the mail, send it!

  • David N.4117

    In your service start method first calculate the difference in hours, minutes between now (i.e. the time that the service starts) and the time that you want the email event to kick off either that day or the next. Next call System.Threading.Thread.Sleep(new Timespan(days, hrs, min, sec) ) and enter in the time to wait before the next email send.

    Next create a new thread with an infinite loop (i.e. while(true)) that first sends the email then calls the Thread.Sleep(new Timespan(1, 0, 0, 0) so it will sleep for one day untill it sends again.

    I hope that this helps.


  • Kasic Slobodan

    hi,

    it doesn't need to be precise in any way, if it sends a mail some time between 02.00-03.00 or something it's cool..

    Do you have some example code for this

    thanks


  • DrJim

    Hi

    You can create a windows service for this purpose, but if you are just sending a single mail in a day then I will not suggest you a windows service. this is a very small task to be performed for very short time.
    You can create a VB Script file that will be executed by Windows Scheduler and that Script will send an Email. for this purpose you have no need to change the code you have done for sending email.
    If you need any help regarding the suggestion above please let me know at akbarbuneri[at]gmail.com I will send you some example code.
    If it is ASP.NET web application then you must have other stratigy than this one.

    Thank You



  • send mail using timer