Windows服务中的问题 [英] Issue in windows service

查看:70
本文介绍了Windows服务中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行Windows服务,以便在某些情况发生时(例如每个星期三)将自动电子邮件发送到(存储在数据库中)邮件ID,在下面给出service1.cs的代码

I am doing a windows service to send automatic emails to the (stored in database) mail ids when certain condition occurs(say on every Wednesday),the code of the service1.cs is given below

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.
        }
    }
}





但是此服务不会将任何邮件发送到存储的邮件ID.我找不到问题所在.请给您有用的意见.
您的所有建议都是值得的!
谢谢......

john sathish





But this service is not sending any mails to the stored mail ids.I dont find where the problem is.Kindkly give your helpful comments.
All your advices are appreciable !
Thanks....

john sathish

推荐答案

0)是否需要凭据才能通过指定的服务器发送邮件?

1)您是否尝试过使用事件日志来发现您的服务正在执行什么操作以及以什么顺序进行操作?

2)您是否尝试过调试器?

3)您是否尝试过将代码放入控制台应用程序以查看其是否有效? (也更容易以这种方式调试)
0) Do you need credentials to send mail through the specified server?

1) Have you tried using the event log to discover what your service is dowing and in what order?

2) Have you ever tried the debugger?

3) Have you tried putting the code into a console app to see if it even works? (Easier to dbug this way as well)


这不是服务的目的,这是计划任务的目的.现在唯一一次发送任何内容的方法就是在星期三启动该服务,然后仅在启动后才执行此操作.实际上,它应该是计划在每个星期三运行的控制台应用程序,而不是一项服务.

如果要保留它的服务,则需要使计时器或线程或其他东西定期唤醒并运行您的电子邮件代码,但仅在星期三进行.
This isn''t what services are for, this is what scheduled tasks are for. The way this is implemented right now the only time that it will send anything is if the service is started on a Wednesday, and then it''ll only do it''s thing ONCE when it''s started. This really looks like it should be a console app that''s scheduled to run every Wednesday instead of being a service.

If you want to keep it a service you''ll need to make a timer or thread or something to wake up periodically and run your email code, but only on Wednesdays.


这篇关于Windows服务中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆