automatic email sending

Joined
Aug 22, 2008
Messages
1
Reaction score
0
hi,
i hav a windows service which i installed in my server, so that i can perform automatic email sending. but i would like to set timer in windows service.ie. i want the email to be send at regular intervals which i wil fetch from my database.like 3, 6, 9, 12 months.Also like in outlook , i too have a scheduler in my application, so i wud like to use the same windows service for sending email instead of alarm remainder in outlook. i am pasting my windows service code here.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Web.Mail;
using System.Data.OleDb;
using System.Data.SqlClient;
namespace GoodDay
{
public class GoodDay : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Data.OleDb.OleDbConnection conn;
private System.Data.SqlClient.SqlConnection conn1;
DateTime mdt=DateTime.Now;
public GoodDay()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call
}

// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;

// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new GoodDay() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.conn = new System.Data.OleDb.OleDbConnection();
//
// conn
//
this.conn.ConnectionString = @"Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Data Source=""C:\Documents and Settings\omega\My Documents\db2.mdb"";Mode=Share Deny None;Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1";
//
// GoodDay
//
this.CanHandlePowerEvent = true;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.ServiceName = "GoodDay";

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
///
DataSet ds = new DataSet();
protected override void OnStart(string[] args)
{
conn1 = new SqlConnection(@"Server=VN1098;UID=NCCPTNEW;pwd=john123;Database=dsn_justchai");
conn1.Open();

DateTime dt=DateTime.Now;
//conn.Open();
SqlDataAdapter da=new SqlDataAdapter("select * from Autoemail",conn1);
da.Fill(ds);
if(dt.ToShortDateString()==mdt.ToShortDateString())
{
foreach(DataRow dr in ds.Tables[0].Rows)
{


mdt=DateTime.Now.AddDays(+1);

String mailtxt="";
MailMessage mm = new MailMessage();
mm.BodyFormat = MailFormat.Html;
mm.To = dr["EmailID"].ToString();
mm.From = "(e-mail address removed)";
mm.Subject="Good Day";
mailtxt = "<font face='verdana' color='#FF9900'><b>" + "Hi " + dr["EmailID"].ToString() + "," + "</b></font><br><br>";
mailtxt=mailtxt+"<font face='verdana' color='#FF0000'><b>"+"Good Day." + "</b></font><br><br>";
mailtxt=mailtxt+"<font face='verdana' color='#008080'><b>"+"May today be filled with sunshine and smile, laughter and love." + "</b></font><br><br>";
mailtxt=mailtxt+"<font face='verdana' color='#0000FF'><b>Cheers!" + "<br><br>";
mm.Body = mailtxt;
SmtpMail.SmtpServer="localhost";
SmtpMail.Send(mm);


}

}
}

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top