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