29 July 2010

Automatics email sending using windows service

Written by Amit Panchal, Posted in Blog

Hello friends,
we have seen so many web application which sends us emails notification at some regular time.
eg. if ur subscription is about to expire the system automatically notifies you by a mail.
it is very easy to do using windows service and web service.
to start first creat a web service in your asp.net project.
it will add filename.asmx ans filename.cs file in ur project.
open the filename.cs file and put your sending mail code in function like this.

public string SendNotifications() {
        string msg = "This mail is sent from Mail Scheduler.
        bool temp1=sendmail("toaddress", "fromaddress", "Mail Scheduler", msg);
       //put ur code to send email in sendmail function
        if (temp1)
        {return "Mail Sent"; }
       else { return "Error"; }

now open visual studio and creat new project > windows service
and put the following code in your main service1.cs file

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Threading;
namespace Mail_Scheduler
    public partial class mailScheduler : ServiceBase
        public System.Threading.Timer timer1;
        public mailScheduler()
        protected override void OnStart(string[] args)
            int ticks = 1;
            if (args.Length > 0)
                try { ticks = Convert.ToInt16(args[0]); }
                catch (Exception ex) { }
            ticks = 60000 *60* ticks;
            this.timer1 = new Timer(new TimerCallback(this.timer1_Tick), null, 0,ticks);
            writeLog(DateTime.Now.ToString() + ": service started (Interval="+ticks.ToString()+")");
        protected override void OnStop()
            writeLog(DateTime.Now.ToString() + ": service stopped");
        private void writeLog(string msg)
            FileStream fs = new FileStream(@"c:\temp\mailScheduler.txt",
FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter m_streamWriter = new StreamWriter(fs);
            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
        private void timer1_Tick(object stateInfo)
                WebServiceRef.WebServiceSoapClient obj = new Mail_Scheduler.WebServiceRef.WebServiceSoapClient();
                string result = obj.SendNotifications();
                writeLog(DateTime.Now.ToString() + ": " + result);
            catch (Exception ex) { }
        private void timer2_Tick(object sender, EventArgs e)
            string result = DateTime.Now.ToString();
            writeLog(DateTime.Now.ToString() + "= " + result);

you need to add web reference of the webservice you just made, into ur windows service project.
and can use the method of webservice by creating an object of webservice.
now you are done.just create a setup of your windows service and install it.
to know more about how to create windows service click here
if you have any doubts feel free to comment us

Social Bookmarks

About the Author

Amit Panchal


Amitech Business Solutions

All Events


Comments (0)

Leave a comment

You are commenting as guest. Optional login below.

Cancel Submitting comment...