Search code examples
c#emailfilesystemwatcher

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

    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);
                }                               
            }            
        }
    }
}

Solution

  • 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. Once you correct this, it should work.