Thursday 28 March 2013

How to send mail automatically after specific interval of time


How to send mail automatically after specific interval of time


This article explains how to send mail automatically after a specific interval of time.

.Net provides a Timer Components to execute a particular code after a specific interval of time.

Timer Components is a server based timer that allows you to specify Interval and the Elapsed event is raised in your application. This event to provide regular processing.

Example:
    protected void Page_Load(object sender, EventArgs e)
    {

        System.Timers.Timer time = new System.Timers.Timer();
        time.Start();
        time.Interval = 5*60*1000; //interval in milisecond
        time.Elapsed += time_elapsed;

    }
    public void time_elapsed(object sender, ElapsedEventArgs e)
    {
        string from = "skbadliya@gmail.com";
        //Replace this with your own correct Gmail    Address
        string to = "satish.badliya91@gmail.com";
        //Replace this with the Email Address to  whom you want to send the mail
        string subject = "Mail Testing";
        string body = "Welcome To Keyprompt Technologies Pvt.Ltd.";

        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true;//Gmail works on Server Secured Layer
        //Add the Creddentials- use your own email id and password

        System.Net.NetworkCredential networkcred = new System.Net.NetworkCredential();
        networkcred.UserName = from;
        networkcred.Password = "Your Password";
        client.Port = 587;// Gmail works on this port
        client.Credentials = networkcred;
        client.Send(from, to, subject, body);
       
    }

So, friends enjoy the code.

No comments:

Post a Comment