FileSystemWatcher Windows Services in Windows Server 2008

本文关键字:Windows 2008 Server Services FileSystemWatcher in | 更新日期: 2023-09-27 18:15:17

我的新服务在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);
                }                               
            }            
        }
    }
}

FileSystemWatcher Windows Services in Windows Server 2008

您的SMTP代码中有几个问题,第一个问题是您没有指定发件人,第二个问题是您没有指定收件人,最后您没有向您使用的电子邮件服务器提供凭据。一旦你纠正了这个错误,它就可以工作了。