简体   繁体   English

Windows Server 2008中的FileSystemWatcher Windows服务

[英]FileSystemWatcher Windows Services in Windows Server 2008

i have a problem with my new services in WS 2008, my services run in my lab under Windows 7, no problem, but when implement this services in my server, no run, this service providing data for send later for email, this data is a xls file save in my folder "share". 我在WS 2008中的新服务有问题,我的服务在Windows 7下的实验室中运行,没问题,但是当在服务器中实施此服务时,没有运行,该服务提供了稍后发送给电子邮件的数据,该数据是一个xls文件保存在我的文件夹“ share”中。

    namespace WsEmail
    {

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    public partial class WsMail : ServiceBase
    {
        FileSystemWatcher fsw = new FileSystemWatcher();
        Timer tm = new Timer();
        EventLog evLufran = new EventLog();

        public WsMail()
        {
            // Componentes a Iniciar
            InitializeComponent();
        }

        void tm_Elapsed(object sender, ElapsedEventArgs e)
        {
            //DateTime hr = DateTime.Now;
            //if (hr.Hour == 8)
            //{
                OpenWithStartInfo();
            //}
        }

        public void fsw_Created(object sender, FileSystemEventArgs e)
        {
            try
            {
                Mail(e.FullPath);
            }
            catch (Exception ex)
            {
                if (EventLog.SourceExists("LufranMail"))
                {
                    EventLog.CreateEventSource("LufranMail", "Application");
                    evLufran.WriteEntry(ex.Message, EventLogEntryType.Warning, 234);
                }                
            }               
        }



        protected override void OnStart(string[] args)
        {
            // Creacion del Monitoreo            
            fsw.Path = @"E:\share\";
            fsw.Filter = "*.xls";
            fsw.IncludeSubdirectories = false;
            fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.CreationTime;            
            fsw.EnableRaisingEvents = true;
            fsw.Created += fsw_Created;

            // Timer para Ejecutar el OpenWithStarInfo            
            tm.Interval = 1000 * 50;
            tm.Enabled = true;
            tm.Elapsed += tm_Elapsed;

            //evLufran.Source = "LufranMail";
            //evLufran.Log = "Application";
            //evLufran.EnableRaisingEvents = true;

        }

        protected override void OnStop()
        {
            fsw.EnableRaisingEvents = false; 
        }

        void Mail(string eAdjun)
        {
            MailMessage objMail;
            objMail = new MailMessage();
            objMail.From = new MailAddress("", "Notificaciones Lufran", System.Text.Encoding.UTF8); //Remitente
            objMail.To.Add(""); //Email a enviar 
            objMail.CC.Add(""); //Email a enviar copia
            objMail.Bcc.Add(""); //Email a enviar oculto
            objMail.Subject = "Clientes con Cotizaciones a 4 semenas (Mercancia por llegar)";
            objMail.SubjectEncoding = System.Text.Encoding.UTF8;
            objMail.Body = "Verifique por favor la informacion del archivo excel y remitala a los clientes que correspondan";
            objMail.BodyEncoding = System.Text.Encoding.UTF8;
            objMail.IsBodyHtml = false; //Formato Html del email
            objMail.Attachments.Add(new Attachment(eAdjun));
            SmtpClient SmtpMail = new SmtpClient();
            SmtpMail.Credentials = new System.Net.NetworkCredential("", "");
            SmtpMail.Port = 587; //asignamos el numero de puerto
            SmtpMail.Host = "smtp.gmail.com"; //el nombre del servidor de correo
            SmtpMail.EnableSsl = true;
            /*Captura de Errores*/
            try
            {
                SmtpMail.Send(objMail);
                SmtpMail.Dispose();
                objMail.Dispose();
                try
                {
                    File.Delete(eAdjun);
                }
                catch (Exception ex)
                {
                    if (EventLog.SourceExists("LufranMail"))
                    {
                        EventLog.CreateEventSource("LufranMail", "Application");
                        evLufran.WriteEntry("Archivo no se puede eliminar: " + ex.Message);
                    }

                }                                
            }
            catch (Exception ex)
            {
                if (!EventLog.SourceExists("LufranMail"))
                {
                    EventLog.CreateEventSource("LufranMail", "Application");
                    evLufran.WriteEntry(ex.Message);
                }                
            }
        }

        public void OpenWithStartInfo()
        {
            try
            {
                ProcessStartInfo startInfo = new ProcessStartInfo(@"E:\Share\xComprasLF.fxp");
                startInfo.WindowStyle = ProcessWindowStyle.Minimized;
                Process.Start(startInfo);
            }
            catch (Exception ex)
            {
                if (!EventLog.SourceExists("LufranMail"))
                {
                    EventLog.CreateEventSource("LufranMail", "Application");
                    evLufran.WriteEntry(ex.Message);
                }                               
            }            
        }
    }
}

There are several problems in your SMTP code, the first one is that you don't specify a sender, the second one is that you don't specify a recipient and finally you don't provide your credentials to the email server you use. SMTP代码中存在几个问题,第一个问题是您没有指定发件人,第二个问题是您没有指定收件人,最后您不向使用的电子邮件服务器提供凭据。 Once you correct this, it should work. 一旦更正此问题,它便会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM