using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; using System.Data.OleDb; using System.Net.Mail; namespace AutomaticEmailSystem { public partial class AutomaticEmailSystem : ServiceBase { private System.Data.OleDb.OleDbConnection conn; public AutomaticEmailSystem() { InitializeComponent(); start(); } 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 AutomaticEmailSystem() }; 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 start() { 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:\Email.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 = "AutomaticEmailSystem"; } /* protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); }*/ DataSet ds = new DataSet(); protected override void OnStart(string[] args) { try { // TODO: Add code here to start your service. conn.Open(); OleDbDataAdapter da = new OleDbDataAdapter("select * from Email", conn); da.Fill(ds); string day = Convert.ToString(DateTime.Now.DayOfWeek); if (day == "Wednesday") { foreach (DataRow dr in ds.Tables[0].Rows) { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); mail.From = new MailAddress("compueclinic@gmail.com"); string to = dr["mailid"].ToString(); mail.To.Add(to); mail.Subject = "Stock And Sale Reports"; string mailtxt = ""; mail.IsBodyHtml = true; mailtxt = "Hi " + to + ","; mailtxt = mailtxt + "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='#008080'><b>" + "Find the attached Stock and Sale Reports." + "</b></font><br><br>"; mailtxt = mailtxt + "<font face='verdana' color='#0000FF'><b>Cheers!" + "<br><br>"; mailtxt = mailtxt + "<font face='verdana' color='#0000FF'><b>Regards!" + "<br><br>"; mailtxt = mailtxt + "<font face='verdana' color='#0000FF'><b>Compueclinic@gmail.com" + "<br><br>"; // mm.Body = mailtxt; mail.Body = mailtxt; System.Net.Mail.Attachment attachment,att; attachment = new System.Net.Mail.Attachment("c:\\Stock_Report.xls"); att = new System.Net.Mail.Attachment("c:\\Sale_Report.xls"); mail.Attachments.Add(attachment); mail.Attachments.Add(att); SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential("*****@gmail.com", "******"); SmtpServer.EnableSsl = true; SmtpServer.Send(mail); eventLog1.WriteEntry("Automatic Email Service Started"); } } else { AutomaticEmailSystem aa = new AutomaticEmailSystem(); aa.Stop(); ///////Stop service manually jst } } catch (System.Exception ex) { string excep = ex.ToString(); } } protected override void OnStop() { // TODO: Add code here to perform any tear-down necessary to stop your service. } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)