Tuesday, January 10, 2012

Creating Mail Scheduler in C#

Today while developing a page for sending mail, I was asked to create a scheduler in c# that would send mail on periodically.
I did an R&D for that and resolved the problem creating WindowServices in c#.

Its easy just go thr these steps;

1.Open Visual Studio.net > Select New Project

2.Select Windows Service and give Name as 'MailScheduler' and Click Ok.

3.Right Click the Design and Select Properties and give the Name and ServiceName as

'ScheduleMail' and set AutoLog to 'True'

4.Right Click the Design and select View Code and

Select Project > Add Reference > Select System.Web >Press Ok

5.Now add the following to your code

using System.Web.Mail;

In static void Main()

ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new ScheduleMail()
};
ServiceBase.Run(ServicesToRun);

6. Edit the following code in onstart()

protected override void OnStart(string[] args)
{
eventLogSimple.WriteEntry("ScheduleMail service started at :" + DateTime.Now.ToShortDateString().Trim());
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 30000; //1000 for 1sec so 1hr has 3600sec and 1day has 3600*24 sec
//i.e 86400 sec
//hence for this in ms = 86400000(for one day)
timer.AutoReset = true;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_TimeElapsed);
// timer_TimeElapsed();
timer.Enabled = true;
eventLogSimple.WriteEntry("mail sent successfully at :" + DateTime.Now.ToShortDateString().Trim());

}

Add following method
private static void timer_TimeElapsed(object source, System.Timers.ElapsedEventArgs e)
{
if (DateTime.Now.ToShortDateString().Trim() == "1/10/2012")
{

MailMessage msg = new MailMessage();
msg.To = "toaddress here..";
msg.From = "from address here..";
msg.Subject = "Test Scheduler";
msg.BodyFormat = MailFormat.Text;
msg.Body = "this is a scheduled mail from mail scheduler..";
msg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = "servernamehere...";
msg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = 25;
msg.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;
msg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;

////Most of the times the @domain is necessary with the username
msg.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "username here..";
msg.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "password here..";
SmtpMail.SmtpServer = "servername here..";
SmtpMail.Send(msg);
}
}

7.Go to Design,right click and Select Add Installer ,you will notice two controls

serviceProcessInstaller1,serviceInstaller1

8.Select serviceProcessInstaller1 and go to its properties and change the Account

type to 'Local System'

9. Select serviceInstaller1 and go to its properties and change DisplayName and

ServiceName to 'ScheduleMail' and make the StartType to 'Automatic'

10.Build the Solution

11.Open the Visual Studio .Net Command Prompt and give the path of application

for example C:\bin\Debug and then type InstallUtil MailScheduler.exe

to uninstall, InstallUtil /u MailScheduler.exe

15. After Installation ,right click My Computer and

Select 'Manage'

and Select 'Service and Applications'

and in that Select 'Services'

Select ScheduleMail' and start the Service.

Good Luck!!!

1 comment: