目录

时间:2017-08-07 14:06:56

标签: javascript c#

我正在尝试发送查看目录中lastwritetime的电子邮件警报,并且任何超过15分钟的内容都会发送电子邮件警报。这是我的代码和我想要做的。

        MailMessage CyclopsCentral = new MailMessage();
        SmtpClient client = new SmtpClient(server);
        CyclopsCentral.From = new System.Net.Mail.MailAddress("shawn_swingler@cable.comcast.com");
        CyclopsCentral.To.Add("_spt-eqto-eng-eps_@comcast.com");
        CyclopsCentral.Subject = "Cyclops Central Not Updating";
        CyclopsCentral.Body = "Cyclops Central is not updating, please check the management center.";
        CyclopsCentral.Priority = MailPriority.High;


        try
        {
            string[] files = Directory.GetFiles(@"\\Users\!NCDSPOTVIEW\Desktop\ScreenScrapes\Chicago\ScreenScrape\ScreenScrape.png\");


            foreach (string file in files)
            {
                FileInfo fi = new FileInfo("ScreenScrape.png");
                if (fi.LastWriteTime < DateTime.Now.AddHours(-1))
                    fi.Send(CyclopsCentral);
            }
            //client.LogFileName = "d:\\smtp.txt";
            Console.WriteLine("Start to send email ...");
            //client.Send(CyclopsCentral);
            Console.WriteLine("email was sent successfully");
        }

        catch (Exception ep)
        {
            Console.WriteLine("failed to send email with the following error:");
            Console.WriteLine(ep.Message);
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

您没有使用从Directory.GetFiles()返回的文件列表。试试这个:

foreach (string file in files)
{
    FileInfo fi = new FileInfo(file);
    if (fi.LastWriteTime < DateTime.Now.AddMinutes(-15))
    {
        client.Send(CyclopsCentral);
    }
}

此外,如果您只想要一封电子邮件,无论其发现的文件数量是多少,您都可以在break之后添加client.Send声明。